【问题标题】:NPE when trying to change locale using RadioButton Android尝试使用 RadioButton Android 更改语言环境时的 NPE
【发布时间】:2016-12-20 03:14:55
【问题描述】:

我目前有一个按钮,单击该按钮时会显示一个 AlertDialog 和 2 个 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

如果有任何帮助,我将不胜感激。谢谢。

【问题讨论】:

  • SectionsActivity.java:264 - 那是哪一行?
  • ` if(en.isActivated()){ `

标签: android nullpointerexception radio-button android-alertdialog


【解决方案1】:

在这种情况下,您的问题是 en 为空。 可能您用于初始化它的资源丢失或返回的对象与您预期的不同。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    相关资源
    最近更新 更多