I want to open alert dialog box from menu and in alert dialog box I want check boxes so that user can select the items but I am getting null pointer exception.
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: FATAL EXCEPTION: main Process: com.example.kishan.bulletin, PID: 24928 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference at com.example.kishan.bulletin.MainActivity$2$override.onClick(MainActivity.java:206) at com.example.kishan.bulletin.MainActivity$2$override.access$dispatch(MainActivity.java) at com.example.kishan.bulletin.MainActivity$2.onClick(MainActivity.java:0) at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5769) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)