0

我目前有一个按钮,单击该按钮时会显示AlertDialog2 RadioButtons,一个用于英语,另一个用于西班牙语。当用户单击英语并确认我希望语言为英语时。如果他们选择西班牙语,我希望它是西班牙语。

目前这是我的代码:

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是空值。也许您用来初始化它的资源丢失或返回的对象与您预期的不同。

尝试调试并查看 -v.findViewById(R.id.radioButton1)返回。

于 2013-05-15T01:17:35.093 回答