在我的片段中,我有一个微调器。这是我的代码:
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)