I'm trying to customize an AlertDialog
by adding RadioButton
and CheckBox
to ask for information from users. However, the app crashes when loading the dialog and I get NullPointerException
when adding setOnCheckedChangeListener
to both radio buttons and check boxes. For more details please take a look at my source code and error log below:
public class DashboardActivity extends Activity implements CheckBox.OnCheckedChangeListener {
private UserFunctions userFunctions;
private String preferences = "none";
private String spending = "none";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check login status in database
Intent skip = getIntent();
userFunctions = new UserFunctions(this);
if(userFunctions.isUserLoggedIn(getApplicationContext()) || (skip!=null && skip.getBooleanExtra("IS_SKIPPED", false) == true)){
// user already logged in or skip logging in, show dashboard
setContentView(R.layout.dashboard);
ActionBar actionBar = getActionBar();
actionBar.hide();
if (userFunctions.isUserLoggedIn(getApplicationContext()) && !userFunctions.isInfoEntered()){
// missing user's info about preferences and spending -> show dialog to ask for input
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Additional information")
.setMessage("Please provide more information for better recommendations:");
View view = getLayoutInflater().inflate(R.layout.popup, null);
alert.setView(view);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.spendGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
RadioButton button = (RadioButton) findViewById(checkedId);
spending = button.getText().toString();
}
});
CheckBox westernBox = (CheckBox) findViewById(R.id.WesternInput);
westernBox.setOnCheckedChangeListener(this);
CheckBox asianBox = (CheckBox) findViewById(R.id.AsianInput);
asianBox.setOnCheckedChangeListener(this);
CheckBox buffetBox = (CheckBox) findViewById(R.id.buffetInput);
buffetBox.setOnCheckedChangeListener(this);
CheckBox hotpotBox = (CheckBox) findViewById(R.id.hotpotInput);
hotpotBox.setOnCheckedChangeListener(this);
CheckBox ffoodBox = (CheckBox) findViewById(R.id.fastInput);
ffoodBox.setOnCheckedChangeListener(this);
CheckBox grillBox = (CheckBox) findViewById(R.id.grillInput);
grillBox.setOnCheckedChangeListener(this);
CheckBox sfoodBox = (CheckBox) findViewById(R.id.seafoodInput);
sfoodBox.setOnCheckedChangeListener(this);
CheckBox veganBox = (CheckBox) findViewById(R.id.veganInput);
veganBox.setOnCheckedChangeListener(this);
CheckBox iceBox = (CheckBox) findViewById(R.id.iceInput);
iceBox.setOnCheckedChangeListener(this);
CheckBox cakeBox = (CheckBox) findViewById(R.id.cakeInput);
cakeBox.setOnCheckedChangeListener(this);
CheckBox allBox = (CheckBox) findViewById(R.id.allInput);
allBox.setOnCheckedChangeListener(this);
alert.setNeutralButton("Save", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,final int which) {
DatabaseHandler db = new DatabaseHandler(DashboardActivity.this);
UserFunctions userFunction = new UserFunctions(DashboardActivity.this);
userFunction.updateUser(db.getUserDetails().get("email"), db.getUserDetails().get("name"), db.getUserDetails().get("password"), db.getUserDetails().get("bday"), db.getUserDetails().get("country"), preferences, spending);
}
});
alert.create().show();
}
}else{
// user is not logged in show login screen
Intent login = new Intent(getApplicationContext(), WelcomeActivity.class);
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Closing dashboard screen
finish();
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked==true){
if (buttonView.getId()!=R.id.allInput){
preferences += buttonView.getText().toString() + ", ";
}
else{
preferences += "Asian, Western, buffet, hot pot, grill, fastfood, seafood, vegan, ice-cream, cake";
}
}
else if (!isChecked && preferences.indexOf(buttonView.getText().toString()) > 0){
}
}
Errors shown in Logcat:
02-15 13:07:04.182: E/AndroidRuntime(1795): FATAL EXCEPTION: main 02-15 13:07:04.182: E/AndroidRuntime(1795): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hanu.hgourmet/com.hanu.hgourmet.DashboardActivity}: java.lang.NullPointerException 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.ActivityThread.access$600(ActivityThread.java:141) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.os.Handler.dispatchMessage(Handler.java:99) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.os.Looper.loop(Looper.java:137) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-15 13:07:04.182: E/AndroidRuntime(1795): at java.lang.reflect.Method.invokeNative(Native Method) 02-15 13:07:04.182: E/AndroidRuntime(1795): at java.lang.reflect.Method.invoke(Method.java:511) 02-15 13:07:04.182: E/AndroidRuntime(1795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-15 13:07:04.182: E/AndroidRuntime(1795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-15 13:07:04.182: E/AndroidRuntime(1795): at dalvik.system.NativeStart.main(Native Method) 02-15 13:07:04.182: E/AndroidRuntime(1795): Caused by: java.lang.NullPointerException 02-15 13:07:04.182: E/AndroidRuntime(1795): at com.hanu.hgourmet.DashboardActivity.onCreate(DashboardActivity.java:60) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.Activity.performCreate(Activity.java:5104) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 02-15 13:07:04.182: E/AndroidRuntime(1795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 02-15 13:07:04.182: E/AndroidRuntime(1795): ... 11 more 02-15 13:11:23.641: E/AndroidRuntime(1865): FATAL EXCEPTION: main 02-15 13:11:23.641: E/AndroidRuntime(1865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hanu.hgourmet/com.hanu.hgourmet.DashboardActivity}: java.lang.NullPointerException 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.ActivityThread.access$600(ActivityThread.java:141) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.os.Handler.dispatchMessage(Handler.java:99) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.os.Looper.loop(Looper.java:137) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.ActivityThread.main(ActivityThread.java:5039) 02-15 13:11:23.641: E/AndroidRuntime(1865): at java.lang.reflect.Method.invokeNative(Native Method) 02-15 13:11:23.641: E/AndroidRuntime(1865): at java.lang.reflect.Method.invoke(Method.java:511) 02-15 13:11:23.641: E/AndroidRuntime(1865): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-15 13:11:23.641: E/AndroidRuntime(1865): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-15 13:11:23.641: E/AndroidRuntime(1865): at dalvik.system.NativeStart.main(Native Method) 02-15 13:11:23.641: E/AndroidRuntime(1865): Caused by: java.lang.NullPointerException 02-15 13:11:23.641: E/AndroidRuntime(1865): at com.hanu.hgourmet.DashboardActivity.onCreate(DashboardActivity.java:60) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.Activity.performCreate(Activity.java:5104) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 02-15 13:11:23.641: E/AndroidRuntime(1865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 02-15 13:11:23.641: E/AndroidRuntime(1865): ... 11 more
Please help me find the problems here. Thanks for any help!
use
RadioGroup radioGroup = (RadioGroup)view. findViewById(R.id.spendGroup);
instead of
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.spendGroup);
for getting View's from AlertDialog you will need to use view
or AlertDialog instance