当我单击Governates_nameedt(EditText)时,我有两个想要的数组。在AlertDialog中显示Governate_names,并根据我选择的值,在state_names数组中选择值。当我运行代码时,asList被更改,我在<code>create()中得到一个错误。show() 处于状态_nameedt(编辑文本)。
我的代码
Governorates_names = Arrays.asList("Ad Dakhiliyah"
,"Ad Dhahirah"
,"Al Batinah North"
,"Al Batinah South"
,"Al Buraimi"
,"Al Wusta"
,"Ash Sharqiyah North"
,"Ash Sharqiyah South"
,"Dhofar"
,"Muscat"
,"Musandam");
if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Ad Dakhiliyah"))
state_names = Arrays.asList("Nizwa","Samail","Bahla","Al Hamra","Manah","Izki","Adam" );
else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Ad Dhahirah"))
state_names = Arrays.asList("Ibri","Yanqul","Dhank");
else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Al Batinah North")){
state_names = Arrays.asList("Sohar","Shinas","Liwa","Saham","Al Khaburah","Suwayq");
}else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Al Batinah South")){
state_names = Arrays.asList("Nakhal","Wadi Al Maawil","Al Musanaah","Barka","Rustaq");
}else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Al Buraimi")){
state_names = Arrays.asList("Al Buraimi","Mahdah","Al Sinas");
}else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Al Wusta")){
state_names = Arrays.asList("Haima","Duqm","Mahout","Al Jazur");
}else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Ash Sharqiyah North")){
state_names = Arrays.asList("Ibra","Al-Mudhaibi","Bidiyah","Wadi Bani Khaled","Dema Wa Thaieen","Al Qabil");
}else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Ash Sharqiyah South")){
state_names = Arrays.asList("Masirah","Sur","Jalan Bani Bu Hassan","Jalan Bani Bu Ali","Al Kamil Wal Wafi");
}else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Dhofar")){
state_names = Arrays.asList("Salalah","Taqah","Mirbat","Thumrait","Sadah","Rakhyut","Dhalkut","Muqshin","Shalim and the Hallaniyat Islands","Al-Mazyona");
}else if(String.valueOf(Governorates_nameedt.getText().toString()).equals("Dhofar")){
state_names = Arrays.asList("Muttrah","Bawshar","Seeb","Al Amarat","Qurayyat");
}else if( String.valueOf(Governorates_nameedt.getText().toString()).equals("Musandam")){
state_names = Arrays.asList("Khasab","Bukha","Daba Al Bayah","Madha");
}
final ArrayAdapter<String> Governorates_name_Opt = new ArrayAdapter<>(personal_info.this, android.R.layout.simple_spinner_dropdown_item, Governorates_names);
final List<String> finalOpt1 = Governorates_names;
Governorates_nameedt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(personal_info.this)
.setTitle("Select ")
.setAdapter(Governorates_name_Opt, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Governorates_nameedt.setText(finalOpt1.get(which).toString());
dialog.dismiss();
}
}).create().show();
}
});
final ArrayAdapter<String> state_name_Opt = new ArrayAdapter<>(personal_info.this, android.R.layout.simple_spinner_dropdown_item, state_names);
final List<String> finalOpt2 = state_names;
state_nameedt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(personal_info.this)
.setTitle("Select ")
.setAdapter(state_name_Opt, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
state_nameedt.setText(finalOpt2.get(which).toString());
dialog.dismiss();
}
}).create().show();
}
});
我的错误
05-13 16:42:25.7208190-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:致命异常:android.support.v7.app.AlertController.setupView(AlertController.java:538)的android.support.v7.app.AlertController.setupView(AlertController.java:226)上的android.widget.ArrayAdapter.getCount(ArrayDapter.java:330)上的main java.lang.NullPointerExceptionandroid.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.view.performClick(view.java:4204)在andro.os.handleCallback运行(view.java:17355)android.os.Handler.dispatchMessage(Handler.java:725)在android.os.Looper.loop(Looper.java:137)在android.app.ActivityThread.main(ActivityThread.java:5041)在java.lang.reflect.Method。java.lang.reflect.Method.invoke(本机方法)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(zygoTenit.java:793)在com.android.internal.os.zyoteinit.main(zyotenit.java:560)在dalvik.system.NativeStart。main(本地方法)05-13 16:42:25.724 466-835/system_process W/ActivityManager:Force finishing activity com.example.hamdaalissaei.final_flah/.personal_info
请帮我解决这个问题
从堆栈跟踪中:
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
不是一件神奇的事情,每次编辑它时,它都会调用您创建的(如果有的话)
循环。为此,添加一个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
问题内容: 我在变量范围方面遇到麻烦。 我期望输出到,但结果是。为什么我在方法中更改了数组中的值,但是原来的数组却更改了? 问题答案: Java中的数组是一个对象。当您通过创建数组时,它是在堆上创建的,并且返回一个参考值(类似于C中的指针)并将其分配给您的变量。 在C语言中,这表示为: 当您将该变量传递给方法时,您将传递给该方法中的局部(堆栈)变量分配(复制)的参考值。不会复制数组的内容,仅复制参
我试图更新数组中的值,我尝试这样做:Mongoose,更新对象数组中的值 但这似乎对我不起作用: myLessonDB返回此项: 我正在尝试按所示的课程ID更改课程中的值,但它不起作用 没有错误或任何看起来在数组中找不到对象的内容 有人知道我做错了什么吗?
我对变量的作用域有问题。 我希望输出为,但结果是。为什么我在方法中改变了数组中的值,而原来的数组却改变了?
数组仍然具有它的原始值,是否有任何方法可以通过迭代函数对数组的元素进行写访问?
问题内容: 我是python的新手,所以这个问题可能有点基本。我有一个元组,其中包含以下内容: 我想更改此元组中的第一个值(即),但我知道元组是不可变的,因此将不起作用。我该如何实现? 问题答案: 首先,您需要问,为什么要这样做? 但是可以通过: 但是,如果您需要进行更改,那么最好将其保留为