具有自定义适配器错误的setonclicklistener

时间:2014-03-02 13:44:19

标签: android android-intent android-listview android-fragments android-logcat

我正在为自定义列表视图执行setOnItemClickListener,此调用来自片段活动,我想将此片段中的Json对象传递给我的最终活动。 getJsonObject是一个方法,它为我提供了对应于字符串'on'和'desc'的JSONobject。

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.i("Position","In item click");
            JSONObject job;
            String send;
            View v = arg1;
            String on = ((TextView) v.findViewById(R.id.offerMainTextView)).toString();
            String desc =((TextView) v.findViewById(R.id.offerDescTextView)).toString();
            job = getJsonObject(on, desc);
            Log.i("Position","In item click ad");
            Intent in = new Intent(getActivity().getBaseContext(), Final.class);
            Log.i("Position","In item click ec");
            in.putExtra("job",job.toString());
            Log.i("Position","In item click end");
            startActivity(in);
        }
    });

我的应用程序崩溃了这个logCat错误

03-02 08:25:22.830: E/AndroidRuntime(1674): FATAL EXCEPTION: main
03-02 08:25:22.830: E/AndroidRuntime(1674): Process: in.mally.mallys, PID: 1674
03-02 08:25:22.830: E/AndroidRuntime(1674): java.lang.NullPointerException
03-02 08:25:22.830: E/AndroidRuntime(1674):     at         in.mally.mallys.Trending.getJsonObject(Trending.java:118)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at in.mally.mallys.Trending$1.onItemClick(Trending.java:55)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.os.Handler.handleCallback(Handler.java:733)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.os.Handler.dispatchMessage(Handler.java:95)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.os.Looper.loop(Looper.java:136)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at java.lang.reflect.Method.invokeNative(Native Method)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at java.lang.reflect.Method.invoke(Method.java:515)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-02 08:25:22.830: E/AndroidRuntime(1674):     at dalvik.system.NativeStart.main(Native Method)

这是getJsonObject的代码。

JSONObject getJsonObject(String on,String desc){
    JSONObject response = o;
    try {
        JSONArray jar = response.getJSONArray("offers");
        for (int i = 0; i < jar.length(); i++) {
            try {
                JSONObject job = jar.optJSONObject(i);
                // Log.e("Note", job.get("on").toString());
                // Log.i("Note", job.get("des").toString());
                if ((on.equals(job.getString("on")))&& (desc.equals(job.getString("desc")))){
                    return job;
                }


            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    return null;
}

0 个答案:

没有答案