listview的setOnItemClickListener抛出异常

时间:2015-03-07 10:01:01

标签: java android listview android-asynctask

单击listview时,应用程序会抛出一些异常。为什么会这样? 以下是我的代码。

class LoadProfile扩展了AsyncTask {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    protected String doInBackground(String... args) {
        // Building Parameters
        String json = null;
        PROFILE_URL = "http://www.example.com/list_chat_single_android.php?from_pin="+pin;

        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(PROFILE_URL);
            httppost.setEntity(new UrlEncodedFormEntity(params));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();
            json = EntityUtils.toString(resEntity);

            Log.i("Chat list: ", json.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return json;
    }

    @Override
    protected void onPostExecute(String json) {
        super.onPostExecute(json);
        try{
        chat_all = new JSONObject(json);
        final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
        JSONArray user = chat_all.getJSONArray("chat_all");
        String contains=json.toString();

        if(contains.contains("name"))
        {
            tv1.setVisibility(View.GONE);
            for (int i = 0; i < user.length(); i++) {
                JSONObject object = user.getJSONObject(i);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("name", object.getString("name"));
                name=object.getString("name");
                to_t=object.getString("to_t");;
                map.put("msg", object.getString("msg"));
                map.put("time_t", object.getString("time_t"));
                map.put("photo", "http://www.example.com/"+object.getString("photo"));
                arraylist.add(map);    
            }

            String[] from = {"name", "msg", "time_t", "photo"};
            int[] to = {R.id.textView1, R.id.textView2, R.id.textView3,  R.id.imageView1};

            ListAdapter adapterss = new ChatAdapter(ChatSingle.this,arraylist,R.layout.chat_single_list,from,to);
            lv1.setAdapter(adapterss);

            lv1.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    /*Intent i=new Intent(ChatSingle.this,ChatSingle_start.class);
                    i.putExtra("name", name);
                    i.putExtra("from_pin", pin);
                    i.putExtra("to", to_t);
                    startActivity(i);*/
                    MatrixCursor matrix = (MatrixCursor)arg0.getItemAtPosition(arg2);

                    String selection = matrix.getString(arg2);  
                    Log.d("Position:-",selection);
                }
            });
        }
        else
        {
            tv1.setText("No chat yet!");
        }

        }catch(Exception e)
        {
            e.printStackTrace();
        }

    }
}

以下是我的应用程序抛出的异常。

03-07 15:29:16.849: E/AndroidRuntime(2006): FATAL EXCEPTION: main
03-07 15:29:16.849: E/AndroidRuntime(2006): java.lang.ClassCastException: java.util.HashMap cannot be cast to android.database.MatrixCursor
03-07 15:29:16.849: E/AndroidRuntime(2006):     at com.event.ChatSingle$1.onItemClick(ChatSingle.java:63)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.os.Handler.handleCallback(Handler.java:605)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.os.Looper.loop(Looper.java:137)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at android.app.ActivityThread.main(ActivityThread.java:4340)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at java.lang.reflect.Method.invoke(Method.java:511)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-07 15:29:16.849: E/AndroidRuntime(2006):     at dalvik.system.NativeStart.main(Native Method)

为什么显示此错误。我不知道如何解决它。任何人都可以知道解决方案。

1 个答案:

答案 0 :(得分:0)

您的ChatApdater getItem实现会返回HashMap个实例,而不是MatrixCursor。变化

 MatrixCursor matrix = (MatrixCursor)arg0.getItemAtPosition(arg2);

 HashMap<String, String> matrix = (HashMap<String, String>)arg0.getItemAtPosition(arg2);