我想从菜单打开警告对话框,在警告对话框中我想要复选框,以便用户可以选择项目,但我得到空指针异常。
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
AlertDialog.Builder favourite_box;
CheckBox googlenews,toi,ie,ht
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Initialise_obj();
checkbox_obj();
}
private void checkbox_obj() {
googlenews=(CheckBox)findViewById(R.id.checkBox1);
toi=(CheckBox)findViewById(R.id.checkBox2);
ie=(CheckBox)findViewById(R.id.checkBox3);
ht=(CheckBox)findViewById(R.id.checkBox4);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_fav_list) {
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.favourite_dialog, null);
favourite_box = new AlertDialog.Builder(this);
favourite_box.setView(dialogView);
// historyBox.setMessage("message to be display in alert box");
favourite_box.setPositiveButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(googlenews.isChecked()){
Toast.makeText(getApplicationContext(),"haha",Toast.LENGTH_SHORT).show();
} if(toi.isChecked()){
Toast.makeText(getApplicationContext(),"done",Toast.LENGTH_SHORT).show();
} if(ie.isChecked()){
Toast.makeText(getApplicationContext(),"completed",Toast.LENGTH_SHORT).show();
} if(ht.isChecked()){
Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
}
dialog.cancel();
}
});
favourite_box.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
final AlertDialog alert=favourite_box.create();
alert.setTitle("Favourite");
alert.show();
}
return super.onOptionsItemSelected(item);
}
}
E / AndroidRuntime:致命异常:主要 过程:com.example.kishan.bulletin,PID:24928 java.lang.NullPointerException:尝试调用虚方法' boolean android.widget.CheckBox.isChecked()'在null对象引用上 在com.example.kishan.bulletin.MainActivity $ 2 $ override.onClick(MainActivity.java:206) 在com.example.kishan.bulletin.MainActivity $ 2 $ override.access $ dispatch(MainActivity.java) 在com.example.kishan.bulletin.MainActivity $ 2.onClick(MainActivity.java:0) 在android.support.v7.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:157) 在android.os.Handler.dispatchMessage(Handler.java:111) 在android.os.Looper.loop(Looper.java:207) 在android.app.ActivityThread.main(ActivityThread.java:5769) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
答案 0 :(得分:0)
请勿在{{1}}中调用checkbox_obj()
方法。从onCreate()
移除方法调用并删除方法onCreate()
。
您必须从对话框布局中获取复选框视图。你必须这样做。我已将checkbox_obj()
onOptionsItemSelected()