0

在我的片段中,我有一个微调器。这是我的代码:

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)
4

2 回答 2

3

也许没有定义下拉视图资源会给您带来麻烦,试试这个:

// create adapter
MyArrayAdapter<String> adapter = new MyArrayAdapter(getActivity(),
     android.R.layout.simple_spinner_item, R.id.textView1, priArray);

// set dropdown view and bind the adapter
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
于 2013-09-30T20:52:49.400 回答
0

我遇到了同样的问题,我通过在 Adapter 类中添加以下代码解决了这个问题,就像我们为 getView 函数所做的一样,我们在这里为微调器下拉列表添加了一个视图。

 @Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    ViewHolder viewHolder;
    if (view == null || !(view.getTag() instanceof ViewHolder)) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_single_line_spinner, null);
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }
    SubTest subTest  = getItem(position);
    if (subTest != null) {
        viewHolder.line1.setText(subTest.getTestname().trim());
    }
    return view;
}
于 2017-01-30T09:27:22.497 回答