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