0

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): 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): 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(ネイティブ メソッド) で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): com.android.internal.os.ZygoteInit.main(ZygoteInit) .java:560) 02-15 13:07:04.182: E/AndroidRuntime(1795): dalvik.system.NativeStart.main(ネイティブ メソッド) 02-15 13:07:04.182: E/AndroidRuntime(1795): 原因に:java.lang.NullPointerException 02-15 13:07:04.182: E/AndroidRuntime(1795): 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 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): 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(ネイティブ メソッド) 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): at 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): at 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 に答える