0

我有一个应用程序,它读取外部数据库并提取结果以填充 a ListView,其行由 aTextView和 a组成Button。但是当我将普通更改为 时ButtonImageButton应用程序在启动后立即停止。我.xml的每一行如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/RelativeLayout1"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal" >

<ImageButton
    android:id="@+id/listButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="41dp"
    android:layout_marginTop="16dp"
    android:background="@null"
    android:src="@drawable/ciplus" />

<TextView
    android:id="@+id/listText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/listButton"
    android:layout_marginRight="56dp"
    android:layout_toLeftOf="@+id/listButton"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

在标签旁边ImageButton,我还收到一条警告:[Accessibility] Missing contentDescription图片上的属性。我不确定这是否是原因。

如果它只是一个Button,程序运行良好。我不知道为什么将其更改为ImageButton会导致问题。提前致谢。

这是java代码:

public class MainActivity extends Activity {

    static final private String LOG_TAG = "main";
    private ArrayList<Content> aList;

    private class Content{

        Content() {};
        public String title;
        public String url;
    }

    private class MyAdapter extends ArrayAdapter<Content>{

        int resource;


        public MyAdapter(Context _context, int _resource, List<Content> titles) {
            super(_context, _resource, titles);
            resource = _resource;
        //  this.context = _context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout newView;

            final Content content = getItem(position);

            // Inflate a new view if necessary.
            if (convertView == null) {
                newView = new LinearLayout(getContext());
                String inflater = Context.LAYOUT_INFLATER_SERVICE;
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
                vi.inflate(resource,  newView, true);
            } else {
                newView = (LinearLayout) convertView;
            }

            // Fills in the view.
            TextView tv = (TextView) newView.findViewById(R.id.listText);
            Button b = (Button) newView.findViewById(R.id.listButton);
            tv.setText(content.title);

            // Sets a listener for the button, and a tag for the button as well.
            b.setTag(Integer.toString(position));
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Reacts to a button press.
                    Intent intent = new Intent(MainActivity.this, WebPage.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("URL", content.url);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
            return newView;
        }       
    }

    class MyAsyncTask extends AsyncTask<String, String, String> {
         private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
         InputStream inputStream = null;
         String result = ""; 
         Content content;

         protected void onPreExecute() {
             super.onPreExecute();
             progressDialog.setMessage("Downloading the news...");
             progressDialog.show();
             progressDialog.setOnCancelListener(new OnCancelListener() {
                 public void onCancel(DialogInterface arg0) {
                     MyAsyncTask.this.cancel(true);
                 }
             });
         }

         @Override
         protected String doInBackground(String... params) {

             String url_select = params[0];

             ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

             try {
                 // Set up HTTP post
                 // HttpClient is more then less deprecated. Need to change to URLConnection
                 HttpClient httpClient = new DefaultHttpClient();

                 HttpPost httpPost = new HttpPost(url_select);
                 httpPost.setEntity(new UrlEncodedFormEntity(param));
                 HttpResponse httpResponse = httpClient.execute(httpPost);
                 HttpEntity httpEntity = httpResponse.getEntity();

                 // Read content & Log
                 inputStream = httpEntity.getContent();
                } catch (UnsupportedEncodingException e1) {
                    Log.e("UnsupportedEncodingException", e1.toString());
                    e1.printStackTrace();
                } catch (ClientProtocolException e2) {
                    Log.e("ClientProtocolException", e2.toString());
                    e2.printStackTrace();
                } catch (IllegalStateException e3) {
                    Log.e("IllegalStateException", e3.toString());
                    e3.printStackTrace();
                } catch (IOException e4) {
                    Log.e("IOException", e4.toString());
                    e4.printStackTrace();
                }
             // Convert response to string using String Builder
             try {
                 BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
                 StringBuilder sBuilder = new StringBuilder();
                 String line = null;
                 while ((line = bReader.readLine()) != null) {
                     sBuilder.append(line + "\n");
                 }
                 inputStream.close();
                 result = sBuilder.toString();
             } catch (Exception e) {
                 Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
             }
             return result;
         } // protected Void doInBackground(String... params)


         protected void onPostExecute(String result) {
             //parse JSON data
             try {
                 super.onPostExecute(result);
                 Log.i(LOG_TAG, result);
                 JSONObject object = new JSONObject(result);
                 JSONArray jArray = object.getJSONArray("sites");
                 for(int i=0; i < jArray.length(); i++) {
                     JSONObject jObject = jArray.getJSONObject(i);
                     content = new Content();
                     if (jObject.has("title") && jObject.has("url")){
                         content.title = jObject.getString("title");
                         content.url = jObject.getString("url");
                         aList.add(content);
                         aa.notifyDataSetChanged();
                     }
                 } // End Loop
                 progressDialog.dismiss();
             } catch (JSONException e) {
            //   progressDialog.dismiss();
                 Log.e("JSONException", "Error: " + e.toString());
             }

         } // protected void onPostExecute(String result)
    }

    private MyAdapter aa;
    private MyAsyncTask loadTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loadTask = new MyAsyncTask();
        loadTask.execute("http://luca-ucsc.appspot.com/jsonnews/default/news_sources.json");
        aList = new ArrayList<Content>();
        aa = new MyAdapter(this, R.layout.list_element, aList);
        ListView myListView = (ListView) findViewById(R.id.listView1);
        myListView.setAdapter(aa);
        aa.notifyDataSetChanged();
    }

    public void refresh(View v){
        if (loadTask.getStatus() == AsyncTask.Status.FINISHED){
            aList.clear();
            aa.notifyDataSetChanged();
            new MyAsyncTask().execute("http://luca-ucsc.appspot.com/jsonnews/default/news_sources.json");
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

这是从午餐到停止的大致 logcat 信息:

04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 3, Width = 6, current total allocated size out of MAX(1024) = 178 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 7, Width = 6, current total allocated size out of MAX(1024) = 186 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 2, current total allocated size out of MAX(1024) = 194 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 15, Width = 9, current total allocated size out of MAX(1024) = 198 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 8, Width = 7, current total allocated size out of MAX(1024) = 209 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 11, current total allocated size out of MAX(1024) = 218 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 14, current total allocated size out of MAX(1024) = 231 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 8, Width = 8, current total allocated size out of MAX(1024) = 247 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 16, Width = 2, current total allocated size out of MAX(1024) = 257 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 0, Width = 0, current total allocated size out of MAX(1024) = 261 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 3, Width = 3, current total allocated size out of MAX(1024) = 263 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 13, current total allocated size out of MAX(1024) = 268 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 3, Width = 4, current total allocated size out of MAX(1024) = 283 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 14, current total allocated size out of MAX(1024) = 289 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 8, current total allocated size out of MAX(1024) = 305 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 4, Width = 3, current total allocated size out of MAX(1024) = 315 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 8, current total allocated size out of MAX(1024) = 320 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 2, Width = 7, current total allocated size out of MAX(1024) = 330 
04-28 00:09:03.859: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 2, Width = 7, current total allocated size out of MAX(1024) = 339 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 6, Width = 8, current total allocated size out of MAX(1024) = 348 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 15, Width = 9, current total allocated size out of MAX(1024) = 358 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 7, current total allocated size out of MAX(1024) = 369 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 9, current total allocated size out of MAX(1024) = 378 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 7, Width = 7, current total allocated size out of MAX(1024) = 389 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 11, Width = 10, current total allocated size out of MAX(1024) = 398 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 9, current total allocated size out of MAX(1024) = 410 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 9, current total allocated size out of MAX(1024) = 421 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 3, current total allocated size out of MAX(1024) = 432 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 3, current total allocated size out of MAX(1024) = 437 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 3, Width = 5, current total allocated size out of MAX(1024) = 442 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 11, Width = 9, current total allocated size out of MAX(1024) = 449 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 10, Width = 5, current total allocated size out of MAX(1024) = 460 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 15, Width = 9, current total allocated size out of MAX(1024) = 467 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 10, Width = 14, current total allocated size out of MAX(1024) = 478 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 18, Width = 5, current total allocated size out of MAX(1024) = 494 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 18, Width = 5, current total allocated size out of MAX(1024) = 13 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 11, Width = 9, current total allocated size out of MAX(1024) = 494 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 2, Width = 9, current total allocated size out of MAX(1024) = 505 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 10, Width = 9, current total allocated size out of MAX(1024) = 516 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 11, Width = 9, current total allocated size out of MAX(1024) = 527 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 11, Width = 9, current total allocated size out of MAX(1024) = 538 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 8, current total allocated size out of MAX(1024) = 549 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 6, current total allocated size out of MAX(1024) = 559 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 10, Width = 14, current total allocated size out of MAX(1024) = 567 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 11, Width = 8, current total allocated size out of MAX(1024) = 583 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 9, current total allocated size out of MAX(1024) = 593 
04-28 00:09:03.869: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 10, Width = 8, current total allocated size out of MAX(1024) = 604 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 3, current total allocated size out of MAX(1024) = 614 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 11, current total allocated size out of MAX(1024) = 619 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 15, Width = 8, current total allocated size out of MAX(1024) = 632 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 9, Width = 8, current total allocated size out of MAX(1024) = 642 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 9, current total allocated size out of MAX(1024) = 652 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 9, current total allocated size out of MAX(1024) = 663 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 9, current total allocated size out of MAX(1024) = 674 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 11, current total allocated size out of MAX(1024) = 685 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 18, Width = 4, current total allocated size out of MAX(1024) = 698 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 18, Width = 4, current total allocated size out of MAX(1024) = 20 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 11, current total allocated size out of MAX(1024) = 698 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 16, current total allocated size out of MAX(1024) = 711 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 19, Width = 6, current total allocated size out of MAX(1024) = 729 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 19, Width = 6, current total allocated size out of MAX(1024) = 26 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 11, current total allocated size out of MAX(1024) = 729 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 14, current total allocated size out of MAX(1024) = 742 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 11, current total allocated size out of MAX(1024) = 758 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 11, current total allocated size out of MAX(1024) = 771 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 10, current total allocated size out of MAX(1024) = 784 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 15, Width = 12, current total allocated size out of MAX(1024) = 796 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 10, current total allocated size out of MAX(1024) = 810 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 11, current total allocated size out of MAX(1024) = 822 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 11, current total allocated size out of MAX(1024) = 835 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 14, Width = 10, current total allocated size out of MAX(1024) = 848 
04-28 00:09:03.879: E/OpenGLRenderer(14018): HUAWEI_DEBUG: glyph's Height = 13, Width = 12, current total allocated size out of MAX(1024) = 860 
04-28 00:09:03.919: I/ActivityManager(279): Displayed ucsc.luca.listviewexample/com.example.newsapp.MainActivity: +467ms
04-28 00:09:04.009: W/SignalStrength(356): getLTELevel - rsrp:2147483647 snr:2147483647 rsrpIconLevel:-1 snrIconLevel:-1
04-28 00:09:04.009: W/SignalStrength(356): getLTELevel - rssi:99 rssiIconLevel:0
04-28 00:09:04.009: W/SignalStrength(356): getGsmLevel=2
04-28 00:09:04.009: W/SignalStrength(356): getLevel=2
04-28 00:09:04.309: W/dalvikvm(14018): threadid=1: thread exiting with uncaught exception (group=0x40d06390)
04-28 00:09:04.849: E/Sensors(279): GsSensor: line +83 ~~~handle===0~~en==0~~!n
04-28 00:09:04.859: E/Sensors(279): GsSensor::setDelay: line +114 ~~~handle===0~~ns==200000000~~
04-28 00:09:04.879: I/WindowManager(279): SCREENLAYOUT_SIZE (1:small, 2:normal, 3:large, 4:xlarge) 2
04-28 00:09:04.879: I/fitatc(485): no preference set
04-28 00:09:04.929: E/OpenGLRenderer(485): HUAWEI_DEBUG: glyph's Height = 8, Width = 3, current total allocated size out of MAX(1024) = 0 
04-28 00:09:04.929: E/OpenGLRenderer(485): HUAWEI_DEBUG: glyph's Height = 0, Width = 0, current total allocated size out of MAX(1024) = 5 
04-28 00:09:04.929: E/OpenGLRenderer(485): HUAWEI_DEBUG: glyph's Height = 7, Width = 6, current total allocated size out of MAX(1024) = 7 
04-28 00:09:04.929: E/OpenGLRenderer(485): HUAWEI_DEBUG: glyph's Height = 8, Width = 4, current total allocated size out of MAX(1024) = 15 
04-28 00:09:04.929: E/OpenGLRenderer(485): HUAWEI_DEBUG: glyph's Height = 9, Width = 6, current total allocated size out of MAX(1024) = 21 
04-28 00:09:06.009: W/SignalStrength(356): getLTELevel - rsrp:2147483647 snr:2147483647 rsrpIconLevel:-1 snrIconLevel:-1
04-28 00:09:06.009: W/SignalStrength(356): getLTELevel - rssi:99 rssiIconLevel:0
04-28 00:09:06.009: W/SignalStrength(356): getGsmLevel=2
04-28 00:09:06.009: W/SignalStrength(356): getLevel=2
04-28 00:09:06.229: W/SignalStrength(457): SignalStrength before validate=SignalStrength: 5 255 -1 -1 -1 -1 -1 99 -32767 -32767 -32767 -32767.0.0 cdma
04-28 00:09:06.229: W/SignalStrength(457): SignalStrength after validate=SignalStrength: 5 255 -120 -160 -120 -1 -1 99 2147483647 2147483647 2147483647 -32767.0.0 cdma
04-28 00:09:06.329: W/lights(279): TP Button Light current value is 60 
04-28 00:09:06.329: W/lights(279): button_backlight:property_get,percent =0
04-28 00:09:06.359: W/MemoryDealer(147): madvise(0x42261000, 16384, MADV_REMOVE) returned Operation not supported on transport endpoint
4

1 回答 1

2

在 xml 布局中,您使用的是“ImageButton”,但在您的自定义“MyAdapter”中,您将其引用为“Button”。这就是为什么您可能会收到“ClassCastException”。

更改 Button b = (Button) newView.findViewById(R.id.listButton);ImageButton b = (ImageButton) newView.findViewById(R.id.listButton);

于 2014-04-28T07:59:11.710 回答