1

我正在尝试显示自定义 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 访问视图(由充气机充气)之间是否存在冲突?

4

2 回答 2

0

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

  1. 尚未调用 Create。当您尝试隐藏视图时,我认为它之前已经显示过。如果是这样,那不是一个选择。
  2. findViewById 没有找到视图。同样的逻辑适用。
  3. 最后一个选项是有人在初始化后将其设置为空。这个很明显,你可能已经分析过了。
  4. 此调用在一个实例上完成,而另一个实例已被初始化。也许 customButtonLayout 是在一个超类中定义的,并且您假设它是每个子类型实例上的相同变量。仅当它是静态成员时才适用。
于 2012-08-14T22:32:40.320 回答
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;
        }
于 2012-08-16T04:26:22.027 回答