我正在尝试显示自定义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充气)之间是否存在冲突?
答案 0 :(得分:0)
此行中的NPE表示customButtonLayout为null。由于View是一个类,因此不能为null。看起来你已经意识到了这一点。现在,如果customButtonLayout为null,则此
可能有不同的原因答案 1 :(得分:0)
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;
}