我製作了2個調整器,而我的第二個調整器依賴於第一個調整器。當我點擊第二個微調時,我的程序崩潰。這是我的logcat說什麼:spinner onItemSelected製作問題
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)
{
}
}
你的命名規範是如此醜陋,,,,這兩個微調名稱是相同的,你把secon微調列表的位置? – 2012-03-11 07:38:55
thnx爲你的迴應samir,好吧我會改變名字,而且我已經看到了教程中的人做了2個旋轉器,但有1個監聽器,並且他通過「this」來識別它,就像我爲spinner1做的一樣 – 2012-03-11 07:42:15
您正在將空數組傳遞給數組適配器。請只發布您認爲可能發生錯誤的代碼。所以它會更具可讀性和可追蹤性。 – AAnkit 2012-03-11 08:04:02