我试图通过添加RadioButton
和CheckBox
来自定义AlertDialog
向用户索要信息。然而,当加载对话框时,应用程序崩溃,当将setOnCheckedChangeListener
添加到单选按钮和复选框时,我得到NullPointerException
。欲了解更多详情,请看看我的源代码和错误日志如下:在logcat中所示Android - NullPointerException在setOnCheckedChangeListener
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){
}
}
错误:
02-15 13:07:04.182:E/AndroidRuntime(1795):致命例外:main 02-15 13:07:04.182:E/AndroidRuntime(1795): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.hanu.hgourmet/com.hanu.hgourmet.DashboardActivity}: java.lang.NullPointerException 02-15 13:07:04.182: E/AndroidRuntime(1795):在 andro android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 02-34:02:13:07:04.182:E/AndroidRuntime(1795): 02- 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):在 android.os.Looper.loop(Looper.java:137)02-15 13:07:04.182: E/AndroidRuntime(1795):在 android.app.ActivityThread.main(ActivityThread.java:5039)02-15 13:07:04.182:E/AndroidRuntime(1795):在 java.lang.reflect.Method.invokeNative(Native Method)02- 15 13:07:04.182:E/AndroidRuntime(1795):在 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 /穿心莲内酯idRuntime(1795):引起: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更多02-15 13:11:23.641:E/AndroidRuntime(1865):致命例外:main 02-15 13:11:23.641:E/AndroidRuntime(1865):java.lang.RuntimeException: 无法启动活动 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.ActivityThr ead.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):在 java.lang.reflect.Method.invoke(Method.java:511)02-15 13:11:23.641: E/AndroidRuntime(1865):at com.android。 (ZygoteInit.java:793) 02-15 13:11:23.641:E/AndroidRuntime(1865) :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)引起: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更多
请帮助我在这里找到问题。谢谢你的帮助!
见线没有。 60,看看它在调试,其值为空 – Rohit 2013-02-15 13:27:43
正如我所说,该行的'radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()' – 2013-02-15 13:28:41