在自定义AlertDialog中设置嵌套LinearLayout的可见性时的NPE?

时间:2012-08-14 17:39:10

标签: java android nullpointerexception visibility android-linearlayout

我正在尝试显示自定义AlertDialog。我在主LinearLayout中有一个包含几个按钮的LinearLayout。按钮只能在特定时间显示,所以我试图将布局设置为GONE。但是,这会在下面指定的行处引发NPE。有什么想法吗?

 @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {      
        //case 0 removed for purposes of space
        case 1: 
            LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=layoutInflater.inflate(R.layout.ingamepopup,null);

        if(getPrefs.getBoolean("custom button checked"+gameNum, false)){
            customButtonLayout.setVisibility(View.VISIBLE);

            cb1.setText(Float.toString(getPrefs.getFloat("button 1 value"+gameNum,1)));
            cb2.setText(Float.toString(getPrefs.getFloat("button 2 value"+gameNum,5)));
            cb3.setText(Float.toString(getPrefs.getFloat("button 3 value"+gameNum,10)));
        } else
            customButtonLayout.setVisibility(View.GONE); //Line 172

        scoreChangeDialog = new AlertDialog.Builder(this)
        .setTitle("Score change")
        .setView(view)
        .create();

        return scoreChangeDialog;
        }
        return null;
    }

customButtonLayout在onCreate()中定义:

customButtonLayout = (LinearLayout) findViewById(R.id.llCustomButtonsScoreChange);

在R中定义:

public static final int llCustomButtonsScoreChange=0x7f070015;

LogCat:

      08-14 13:56:07.192: E/AndroidRuntime(10020): FATAL EXCEPTION: main
08-14 13:56:07.192: E/AndroidRuntime(10020): java.lang.NullPointerException
08-14 13:56:07.192: E/AndroidRuntime(10020):    at com.webs.pratia.scorekeeper.InGame.onCreateDialog(InGame.java:172)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.app.Activity.onCreateDialog(Activity.java:2472)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.app.Activity.createDialog(Activity.java:881)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.app.Activity.showDialog(Activity.java:2547)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.app.Activity.showDialog(Activity.java:2514)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at com.webs.pratia.scorekeeper.InGame.onListItemClick(InGame.java:121)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.widget.AdapterView.performItemClick(AdapterView.java:289)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.widget.ListView.performItemClick(ListView.java:3688)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1873)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.os.Handler.handleCallback(Handler.java:587)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.os.Looper.loop(Looper.java:123)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at android.app.ActivityThread.main(ActivityThread.java:4627)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at java.lang.reflect.Method.invoke(Method.java:521)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-14 13:56:07.192: E/AndroidRuntime(10020):    at dalvik.system.NativeStart.main(Native Method)

修改

经过一些测试后,似乎我在ingamepopup.xml中定义的所有变量都返回null(如cb1,cb2,cb3)。 ingamepopup.xml不是活动的主要xml。 setContentView(ingame.xml)和尝试从ingamepopup.xml访问视图(由inflater充气)之间是否存在冲突?

2 个答案:

答案 0 :(得分:0)

此行中的NPE表示customButtonLayout为null。由于View是一个类,因此不能为null。看起来你已经意识到了这一点。现在,如果customButtonLayout为null,则此

可能有不同的原因
  1. 尚未调用Create。当你试图隐藏视图时,我认为之前已经显示过。如果是这样的话,这不是一个选择。
  2. findViewById没有找到视图。同样的逻辑适用。
  3. 最后一个选项是有人在初始化后将其设置为null。这个非常明显,你可能已经对它进行了分析。
  4. 此调用在一个实例上完成,而另一个实例已初始化。也许customButtonLayout是在超类中定义的,并且您假设它在每个子类型实例上都是相同的变量。只有当它是静态成员时才会这样。

答案 1 :(得分:0)

终于找到了问题。因为我试图从AlertDialog中的视图引用对象(正确的术语?),所以我引用了我膨胀的视图:view.findViewById(...)。通过声明findViewById(...),程序试图从加载到主活动的视图中找到ID。新代码如下:

    LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view=layoutInflater.inflate(R.layout.ingamepopup,null); //This is the view that has to be referenced.

            mainLayout = (LinearLayout) view.findViewById(R.id.llScoreChange); 
//notice view.findViewById(...)
            customButtonLayout = (LinearLayout)  view.findViewById(R.id.llCustomButtonsScoreChange);
            plus = (ImageButton)  view.findViewById(R.id.ibPlusPU);
            minus = (ImageButton)  view.findViewById(R.id.ibMinusPU);
            cancel = (ImageButton)  view.findViewById(R.id.ibCancelPU);
            cb1 = (Button)  view.findViewById(R.id.bCustom1PU);
            cb2 = (Button)  view.findViewById(R.id.bCustom2PU);
            cb3 = (Button)  view.findViewById(R.id.bCustom3PU);
            score = (EditText)  view.findViewById(R.id.etScoreChange);

            if(getPrefs.getBoolean("custom button checked"+gameNum, false)){
                customButtonLayout.setVisibility(View.VISIBLE);

                cb1.setText(Float.toString(getPrefs.getFloat("button 1 value"+gameNum,1)));
                cb2.setText(Float.toString(getPrefs.getFloat("button 2 value"+gameNum,5)));
                cb3.setText(Float.toString(getPrefs.getFloat("button 3 value"+gameNum,10)));
            } else{
                customButtonLayout.setVisibility(View.GONE);
            }

            scoreChangeDialog = new AlertDialog.Builder(this)
            .setTitle("Score change")
            .setView(view)
            .create();

            return scoreChangeDialog;
        }