我正在开发一个Android代码,它返回我存储在String数组中的String值。我想使用这个String数组来填充我的一个Spinners。
我的主要目的是根据用户在第一个微调器中选择的内容,我想使用我的String数组填充我的第二个微调器。
然而,当我尝试时,即使我填充适配器,我也会收到NullPointerException
我的Android代码:
public class Mark2 extends Activity
{
private static final String SOAP_ACTION = "http://tempuri.org/getData";
private static final String METHOD_NAME = "getData";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2/getsubject/Service1.asmx";
private Spinner s;
private Spinner s2;
private Spinner s3;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainspinner);
s = (Spinner) findViewById(R.id.spinner1);
s2 = (Spinner) findViewById(R.id.spinner2);
String[] myList4=call();
for(int i=0;i<myList4.length;i++)
System.out.println(myList4[i]);
String[] myList = new String[] {"First half","Second Half"};
String[] myList2=new String[] {
"abc",
"ghf",
"ijk",
};
String[] myList3=new String[] {
"john",
"ann",
"joe",
};
s.setAdapter(new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, myList));
s2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList2));
final ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList4);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList3);
s.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View v,int pos, long id)
{
if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("First half"))
{
s2.setSelection(pos);
s3.setAdapter(adapter3);
}
else
{
s2.setAdapter(adapter2);
s2.setSelection(pos);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
}
public String[] call()
{
SoapPrimitive responsesData = null;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
responsesData = (SoapPrimitive) envelope.getResponse();
System.out.println(" --- response ---- " + responsesData);
} catch (SocketException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println( " ----" + responsesData );
String serviceResponse= responsesData .toString();
String[] temp;
String delimiter = "#";
temp= serviceResponse.split(delimiter);
System.out.println( " ---- length ---- " + temp.length);
return temp;
}
}
我的logcat:
11-17 15:51:09.668: DEBUG/dalvikvm(125): GC_EXPLICIT freed 1220 objects / 68272 bytes in 164ms
11-17 16:20:29.649: ERROR/AndroidRuntime(620): java.lang.NullPointerException
11-17 16:20:29.649: ERROR/AndroidRuntime(620): at com.example.Mark2$1.onItemSelected(Mark2.java:88)
11-17 16:20:29.649: ERROR/AndroidRuntime(620): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
11-17 16:20:29.649: ERROR/AndroidRuntime(620): at android.widget.AdapterView.access$200(AdapterView.java:42)
11-17 16:20:29.649: ERROR/AndroidRuntime(620): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
11-17 16:20:29.649: ERROR/AndroidRuntime(620): at android.os.Handler.handleCallback(Handler.java:587)
11-17 16:20:29.649: ERROR/AndroidRuntime(620): at android.os.Handler.dispatchMessage(Handler.java:92)
编辑:行号:88是s3.setAdapter(adapter3);
答案 0 :(得分:1)
NullPointerException
。
从您的代码中我发现您尚未初始化Spinner
的s3。
你忘了添加:
s3 = (Spinner) findViewById(R.id.spinner3);