更改android

时间:2017-05-13 16:36:19

标签: android arrays

当我点击Governorates_nameedt(EditText)时,我想要的两个数组显示AlertDialog中的Governorates_names,并根据我在state_names中选择值的值,当我运行代码时,Arrays.asList被更改我在{{ 1}}在state_nameedt(EditText)。

我的代码

.create().show();

我的错误

  

05-13 16:42:25.720 8190-8190 / com.example.hamdaalissaei.final_flah W / dalvikvm:threadid = 1:线程退出未捕获的异常(组= 0xa61a1908)   05-13 16:42:25.724 8190-8190 / com.example.hamdaalissaei.final_flah E / AndroidRuntime:FATAL EXCEPTION:main                                                                                       显示java.lang.NullPointerException                                                                                           在android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)                                                                                           在android.widget.ListView.setAdapter(ListView.java:462)                                                                                           在android.support.v7.app.AlertController.setupView(AlertController.java:538)                                                                                           在android.support.v7.app.AlertController.installContent(AlertController.java:226)                                                                                           在android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:257)                                                                                           在android.app.Dialog.dispatchOnCreate(Dialog.java:355)                                                                                           在android.app.Dialog.show(Dialog.java:260)                                                                                           在com.example.hamdaalissaei.final_flah.personal_info $ 6.onClick(personal_info.java:247)                                                                                           在android.view.View.performClick(View.java:4204)                                                                                           在android.view.View $ PerformClick.run(View.java:17355)                                                                                           在android.os.Handler.handleCallback(Handler.java:725)                                                                                           在android.os.Handler.dispatchMessage(Handler.java:92)                                                                                           在android.os.Looper.loop(Looper.java:137)                                                                                           在android.app.ActivityThread.main(ActivityThread.java:5041)                                                                                           at java.lang.reflect.Method.invokeNative(Native Method)                                                                                           在java.lang.reflect.Method.invoke(Method.java:511)                                                                                           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)                                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)                                                                                           at dalvik.system.NativeStart.main(Native Method)   05-13 16:42:25.724 466-835 / system_process W / ActivityManager:强制完成活动com.example.hamdaalissaei.final_flah / .personal_info

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

从堆栈跟踪中:

8190/com.example.hamdaalissaei.final_flah E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) at 

您输入的一个数组似乎为null。从您的代码中,state_names似乎是个问题。也许if else语句中没有一个实际起作用。试试

else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Musandam")){
        state_names = Arrays.asList("Khasab","Bukha","Daba Al Bayah","Madha");
    } else {
        state_names = new List<String>();
}

由于ArrayAdapter所做的是尝试从您输入的数组中找出数组的大小,并且似乎state_names为空。

编辑:state_names似乎是根据EditText Governorates_nameedt的值决定的。 EditText不是一个神奇的东西,每次编辑它都会调用你创建的if else循环。为此,请添加TextWatcher:

Governorates_nameedt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //Put your if else loop HERE
        }
    });

当您在用户输入任何内容之前首次运行代码时,只需先将state_names发送到新的List<String>()以阻止NullPointerError