我制作了两个旋转器,我的第二个旋转器依赖于第一个旋转器。当我点击第二个微调器时,我的程序崩溃了。这就是我的logcat所说的:
03-11 12:09:35.076: ERROR/AndroidRuntime(367): FATAL EXCEPTION: main
03-11 12:09:35.076: ERROR/AndroidRuntime(367): java.lang.NullPointerException
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:328)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.ListView.onMeasure(ListView.java:1127)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.view.View.measure(View.java:8313)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
03-11 12:09:35.076: ERROR/AndroidRuntime(367): at android.view.View.measure(View.java:8313)
这是我的代码:
package com.test;
import java.io.IOException;
import org.json.JSONException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class SearchActivity extends Activity implements AdapterView.OnItemSelectedListener
{
boolean flag=true;
Spinner spinner;
public String [] doctorsname=new String[1000];
String[] items = {"psychologist", "neurologist","dentist"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchact);
for(int i=0;i<1000;i++)
doctorsname[i]=null;
Button b=(Button)findViewById(R.id.button2);
Button b1=(Button)findViewById(R.id.button3);
spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(SearchActivity.this,DocprofileextActivity.class);
startActivity(i);
}
});
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent i = new Intent(SearchActivity.this,DocprofileextActivity.class);
startActivity(i);
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
if(parent==spinner)
{
Toast toast = Toast.makeText(this, items[position], 2000);
toast.show();
logintosever obj1=new logintosever();
try
{
doctorsname=obj1.searchbycategory(items[position]);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
flag=false;
}
Spinner spinner = (Spinner) findViewById(R.id.doctorsname);
if(doctorsname!=null)
{
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,doctorsname);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setEnabled(true);
spinner.setAdapter(adapter);
}
else
{
spinner.setEnabled(false);
}
}
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
public void onItemSelected2(AdapterView<?> parent, View view, int position,long id)
{
}
}
答案 0 :(得分:0)
我首先向你发表评论,因为它们看起来更重要:
关于你的问题我不是很确定但是它的NullPointerException意味着你试图在一个不接受null的地方使用一个带空值的变量 也许会尝试将你的吐司变量更改为 -
Toast toast = Toast.makeText(this, "item:"+items[position], 2000);
所以你不能把null放在吐司里面。
答案 1 :(得分:0)
主要问题是您的数组大小与您的适配器不匹配,假设您的数组是10个元素,而您的适配器只有5个元素,
装置 string [10] = {“a”,“b”,“c”,“d”,“e”};