0

我正在尝试AlertDialog通过添加RadioButtonCheckBox询问用户信息来自定义一个。但是,应用程序在加载对话框时崩溃,并且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): 致命异常: 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): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 02 -15 13:07:04.182: E/AndroidRuntime(1795): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 02-15 13:07:04.182: E/AndroidRuntime(1795): 在 android.app .ActivityThread.access$600(ActivityThread.java:141) 02-15 13:07:04.182: E/AndroidRuntime(1795): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 02-15 13: 07:04.182:E/AndroidRuntime(1795):在 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): 在 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): 在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经过: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): 在 android.app.Activity.performCreate(Activity.java:5104) 02-15 13:07:04.182: E/AndroidRuntime(1795): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 02-15 13:07:04.182: E/AndroidRuntime(1795): 在 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): 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): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 android .app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 android.app.ActivityThread.access$600(ActivityThread.java:141) 02-15 13: 11:23.641: E/AndroidRuntime(1865): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 android.os.Handler .dispatchMessage(Handler.java:99) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 android.os.Looper.loop(Looper.java:137) 02-15 13:11:23.641: E /AndroidRuntime(1865): 在 android.app.ActivityThread.main(ActivityThread.java:5039) 02-15 13:11:23.641:E/AndroidRuntime(1865): 在 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): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 dalvik.system.NativeStart.main (本机方法)02-15 13:11:23.641:E/AndroidRuntime(1865):引起:java.lang.NullPointerException 02-15 13:11:23.641:E/AndroidRuntime(1865):在 com.hanu.hgourmet .DashboardActivity.onCreate(DashboardActivity.java:60) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 android.app.Activity.performCreate(Activity.java:5104) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 02-15 13:11:23.641: E/AndroidRuntime(1865): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 02-15 13:11:23.641: E/AndroidRuntime(1865): ... 11 更多

请帮我在这里找到问题。谢谢你的帮助!

4

2 回答 2

1

采用

RadioGroup radioGroup = (RadioGroup)view. findViewById(R.id.spendGroup);

代替

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.spendGroup);

要从 AlertDialog 获取视图,您需要使用view或 AlertDialog 实例

于 2013-02-15T13:32:19.507 回答
0

请试试这个

RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.spendGroup);
于 2013-02-15T13:33:30.703 回答