Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android中setAdapter步骤期间ArrayAdapter的NullPointerException_Java_Android_Nullpointerexception - Fatal编程技术网

Java android中setAdapter步骤期间ArrayAdapter的NullPointerException

Java android中setAdapter步骤期间ArrayAdapter的NullPointerException,java,android,nullpointerexception,Java,Android,Nullpointerexception,可能重复: 这是我的密码 String[] city_array = response_city.toString().split(","); Log.d("city array", city_array[0]); ArrayAdapter<String> cityArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line); Log.d(

可能重复:

这是我的密码

String[] city_array = response_city.toString().split(",");  
Log.d("city array", city_array[0]);  
ArrayAdapter<String> cityArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);  
Log.d("city array adapter","count1 = " + cityArrayAdapter.getCount());  
cityArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
int length = city_array.length;  
for (int i = 0; i < length; i++)
    cityArrayAdapter.add(city_array[i]);  
Log.d("city array adapter","count2 = " + cityArrayAdapter.getCount());  
city_spinner.setAdapter(cityArrayAdapter);  
String[]city\u array=response\u city.toString().split(“,”);
Log.d(“城市数组”,城市数组[0]);
ArrayAdapter cityArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u下拉列表\u item\u 1line);
Log.d(“城市阵列适配器”、“count1=“+cityArrayAdapter.getCount());
cityaryadapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
int length=city_array.length;
for(int i=0;i

第一步中的response\u city是一个Web服务器的响应,其中包含“pune,mumbai,bangalore”

我看不出city\u spinner在哪里初始化。Log city_微调器,如果为空,则可以识别问题。空指针异常通常意味着某些东西没有初始化,而且似乎是它造成的。

如下更改

String[] city_array = response_city.toString().split(",");  
Log.d("city array", city_array[0]);  
ArrayLlist<String> items = new ArrayList<String>();

int length = city_array.length;  
for (int i = 0; i < length; i++)
items.add(city_array[i]);

ArrayAdapter<String> cityArrayAdapter = new ArrayAdapter<String>(this,    
android.R.layout.simple_dropdown_item_1line, items);  
Log.d("city array adapter","count1 = " + cityArrayAdapter.getCount());  
cityArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  

Log.d("city array adapter","count2 = " + cityArrayAdapter.getCount());  
city_spinner.setAdapter(cityArrayAdapter);   
String[]city\u array=response\u city.toString().split(“,”);
Log.d(“城市数组”,城市数组[0]);
ArrayList项=新的ArrayList();
int length=city_array.length;
for(int i=0;i
城市\u微调器检查是否正确分配了XML视图……

响应\u城市在第一步中是来自包含“普纳、孟买、班加罗尔”的Web服务器的响应count1的值是0,count2的值是3…..因此它应该在最后一步填充city_微调器…但它给出了nullpointerexception…请帮助从logcat发布错误日志并简要说明您的问题。post logcat error..我认为使用它的小部件可能有问题实际上我没有将它粘贴到这里,但我已初始化它微调器城市微调器=(微调器)findViewById(R.id.city\u微调器)@Archie.bpgc:并验证它不为null?我是否应该添加其他内容???微调器city\u微调器=(微调器)findViewById(R.id.city\u微调器);在上面的代码项是一个字符串数组对吗???在这种情况下,我不需要一个for循环…我可以直接给出第一行的city_数组…实际上我试过了…它给了我同样的错误