我正在尝试通过添加AlertDialog
和RadioButton
来自定义CheckBox
以向用户请求信息。但是,加载对话框时应用程序崩溃,并且在向单选按钮和复选框添加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){
}
}
Logcat中显示的错误:
02-15 13:07:04.182:E / AndroidRuntime(1795):致命异常:主要 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):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):引起: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):致命异常:主要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.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):引起: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更多
请帮我解决这里的问题。谢谢你的帮助!
答案 0 :(得分:1)
使用
RadioGroup radioGroup = (RadioGroup)view. findViewById(R.id.spendGroup);
而不是
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.spendGroup);
要从AlertDialog获取View,您需要使用view
或AlertDialog实例
答案 1 :(得分:0)
请试试这个
RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.spendGroup);