0

AlertDialog現在、クリックすると 2が表示されるボタンがありRadioButtons、1つは英語用、もう1つはスペイン語用です。ユーザーが英語をクリックして、言語を英語にしたいことを確認したとき。彼らがスペイン語を選ぶなら、私はそれをスペイン語にしたい.

現在、これは私のコードです:

langBtn = (Button)findViewById(R.id.langBtn);
            langBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), "Change Language Button", Toast.LENGTH_SHORT).show();


                    //Alert Dialog with 2 options English or Spanish.
                    AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
                    alert.setTitle("Language");
                    alert.setMessage("Please select a language");

                    View myView = getLayoutInflater().inflate(R.layout.language_check, null);
                    alert.setView(myView);

                    final RadioButton en = (RadioButton)v.findViewById(R.id.radioButton1);
                    final RadioButton es = (RadioButton)v.findViewById(R.id.radioButton2);

                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if(en.isActivated()){
                                //language change to english
                                Locale locale = new Locale("en");
                                Locale.setDefault(locale);
                                Configuration config = new Configuration();
                                config.locale = locale; 
                                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                                SectionsActivity.this.setContentView(R.layout.activity_sections);
                                Intent i = new Intent(getApplicationContext(), SectionsActivity.class);
                                startActivity(i);
                            }else{
                                //language change to spanish
                                Locale locale = new Locale("es");
                                Locale.setDefault(locale);
                                Configuration config = new Configuration();
                                config.locale = locale; 
                                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                                SectionsActivity.this.setContentView(R.layout.activity_sections);
                                Intent i = new Intent(getApplicationContext(), SectionsActivity.class);
                                startActivity(i);
                            }

                        }
                    });

                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //Do Nothing

                        }
                    });

                    alert.show();


                }
            });

            return true;
}

これは私のスタックトレースです:

    05-15 01:28:27.334: E/AndroidRuntime(29846): FATAL EXCEPTION: main
05-15 01:28:27.334: E/AndroidRuntime(29846): java.lang.NullPointerException
05-15 01:28:27.334: E/AndroidRuntime(29846):    at com.example.waitronproto9.SectionsActivity$11$1.onClick(SectionsActivity.java:264)
05-15 01:28:27.334: E/AndroidRuntime(29846):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
05-15 01:28:27.334: E/AndroidRuntime(29846):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 01:28:27.334: E/AndroidRuntime(29846):    at android.os.Looper.loop(Looper.java:137)
05-15 01:28:27.334: E/AndroidRuntime(29846):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-15 01:28:27.334: E/AndroidRuntime(29846):    at java.lang.reflect.Method.invokeNative(Native Method)
05-15 01:28:27.334: E/AndroidRuntime(29846):    at java.lang.reflect.Method.invoke(Method.java:511)
05-15 01:28:27.334: E/AndroidRuntime(29846):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run

助けていただければ幸いです。ありがとう。

4

1 に答える 1

0

その場合、あなたの問題はそれenがnullであることです。おそらく、初期化に使用しているリソースが見つからないか、予想とは異なるオブジェクトを返します。

デバッグして、何v.findViewById(R.id.radioButton1)が返されるかを確認してください。

于 2013-05-15T01:17:35.093 に答える