片段抛出nullpointer

时间:2014-06-17 17:44:16

标签: android nullpointerexception

我的fragment位于navigation bar。 当我在“activity_spar”fragment之前的片段中时,它开始加载fragment本身。 然而它给我一个nullpointer exception我无法追溯。

它在line 180上显示,其中包含以下代码:

my_layout.addView(cb,params);

跑过debugger表明我的参数不是空的而我的cb也不是。 任何人都可以解释如何追溯这个吗?

06-17 19:38:21.431: E/AndroidRuntime(25923): FATAL EXCEPTION: main
06-17 19:38:21.431: E/AndroidRuntime(25923): Process: info.androidhive.jsonparsen, PID: 25923
06-17 19:38:21.431: E/AndroidRuntime(25923): java.lang.NullPointerException
06-17 19:38:21.431: E/AndroidRuntime(25923):    at info.androidhive.jsonparsen.activity_Spar$downloadJsonitems.onPostExecute(activity_Spar.java:180)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at info.androidhive.jsonparsen.activity_Spar$downloadJsonitems.onPostExecute(activity_Spar.java:1)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at android.os.AsyncTask.finish(AsyncTask.java:632)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at     android.os.Looper.loop(Looper.java:136)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at android.app.ActivityThread.main(ActivityThread.java:5103)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at java.lang.reflect.Method.invoke(Method.java:515)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
06-17 19:38:21.431: E/AndroidRuntime(25923):    at dalvik.system.NativeStart.main(Native Method)


//AsyncTask<Void,Void,Void>
LinearLayout my_layout = (LinearLayout) getActivity().findViewById(R.id.sparstoretest); 
    LinearLayout my_checked_layout = (LinearLayout) getActivity().findViewById(R.id.sparstorechecked);
    LinearLayout ImageView = (LinearLayout) getActivity().findViewById(R.id.sparstoresettings);
    LinearLayout ImageView2 = (LinearLayout) getActivity().findViewById(R.id.sparstoresettings2);

@Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  ViewGroup.LayoutParams.WRAP_CONTENT);
        params.topMargin=2;

        for (int n = 0; n < listitems.size(); n++) {
            CheckBox cb = new CheckBox(getActivity());

            cb.setId(Integer.parseInt(listitems.get(n).get("cbid")));
            cb.setText(listitems.get(n).get("product"));
            cb.setTextColor(Color.BLACK);
            ImageView image = new ImageView(getActivity());
            image.setImageResource(R.drawable.ic_action_discard);
            image.setId(Integer.parseInt(listitems.get(n).get("cbid")));
            image.setLayoutParams(params);
            image.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    showpopup(v.getId());
                }
            });
            Log.i("activity spar", "storename" + storename);
            if (listitems.get(n).get("state").toString().equals("1") && listitems.get(n).get("store").toString().equals(storename)) {
                cb.setChecked(true);
                my_checked_layout.addView(cb,params);
                cb.setBackgroundColor(Color.argb(255, 184, 239, 220));
                cb.setTextColor(Color.rgb(255, 255, 255));
                image.setBackgroundColor(Color.rgb(184,239,220));

                ImageView2.addView(image);
            } else if (listitems.get(n).get("store").toString().equals(storename)){
                cb.setChecked(false);
                cb.setBackgroundColor(Color.argb(255, 19, 202, 140));
                cb.setTextColor(Color.rgb(255, 255, 255));
                my_layout.addView(cb,params);
                image.setBackgroundColor(Color.rgb(19,202,140));
                ImageView.addView(image);
            }

1 个答案:

答案 0 :(得分:0)

您的sparstoretest布局是否在该活动中定义?您正在获取nullpointer,因为my_layout对象为NULL。检查您是否正在膨胀正确的布局,并在该活动/片段xml中定义了sparstoretest。