在我的片段中,我有一個微調框。這裏是我的代碼:單擊微調框時獲得空指針
public class TaskFragment extends Fragment implements OnItemSelectedListener{
private Spinner spinner;
String tag="[TaskFragment]";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.task_fragment, container,
false);
EditText titleEdit = (EditText) fragmentView
.findViewById(R.id.editText1);
titleEdit.requestFocus();
String[] priArray = new String[] { "بسیار زیاد", "زیاد", "متوسط", "کم",
"بسیار کم" };
String[] statusArray = new String[] { "شروع کار", "در حال انجام",
"متوقف", "لغو", "اتمام" };
spinner = (Spinner) fragmentView.findViewById(R.id.spinner1);
spinner.setAdapter(new MyArrayAdapter(getActivity(),
android.R.layout.simple_spinner_item, R.id.textView1, priArray));
spinner.setOnItemSelectedListener(this);
// --------------------------
return fragmentView;
}
private class MyArrayAdapter extends ArrayAdapter<String> {
private String[] objects;
private Context context;
public MyArrayAdapter(Context context, int resource,
int textViewResourceId, String[] objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.spinner_row, parent,false);
MyTextView tv = (MyTextView) view.findViewById(R.id.textView1);
tv.setText(objects[position]);
return view;
}
}
@Override
public void onStart() {
Log.d("checking", "[TaskFragment]onstart");
super.onStart();
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("checking", tag+"position: "+arg2);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
我在我的佈局中有一個spinner1 id的微調器。一切都很好,並微調器顯示的第一個項目很好,但是當我點擊它,選擇不同的項目出現異常:
09-30 23:54:29.249: E/AndroidRuntime(16399): FATAL EXCEPTION: main
09-30 23:54:29.249: E/AndroidRuntime(16399): java.lang.NullPointerException
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:337)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:333)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.AbsListView.obtainView(AbsListView.java:1582)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.ListView.onMeasure(ListView.java:1181)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.View.measure(View.java:8313)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.View.measure(View.java:8313)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-30 23:54:29.249: E/AndroidRuntime(16399): at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.View.measure(View.java:8313)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.View.measure(View.java:8313)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.View.measure(View.java:8313)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.os.Looper.loop(Looper.java:130)
09-30 23:54:29.249: E/AndroidRuntime(16399): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-30 23:54:29.249: E/AndroidRuntime(16399): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 23:54:29.249: E/AndroidRuntime(16399): at java.lang.reflect.Method.invoke(Method.java:507)
09-30 23:54:29.249: E/AndroidRuntime(16399): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-30 23:54:29.249: E/AndroidRuntime(16399): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-30 23:54:29.249: E/AndroidRuntime(16399): at dalvik.system.NativeStart.main(Native Method)
你得到的線353空指針重新檢查你的代碼:),也許那就是缺少 –
@Warlock TextView的 - 再試一次......這不是他的代碼它的指向。 – dymmeh
@dymmeh呃正確它在ArrayAdapter.java –