单击Android Spinner仅在模拟器上抛出NullPointerException

zac*_*ach 1 android emulation nullpointerexception spinner

我有一个带动态项目的Spinner.通过单击微调器,它将加载一组新项目.它在实际设备上完美运行.但是当我在任何模拟器上运行应用程序时,它会在我单击微调器后抛出NullPointerException.我没有找到任何人的任何解决方案.有谁可以帮助我吗?

以下是LogCat的错误(不同设备和API级别的所有仿真器的错误都是相同的 - 但不会发生在真实设备上).如你所见,它并没有指向我的代码的任何一行.

java.lang.NullPointerException
at android.widget.Spinner.makeAndAddView(Spinner.java:534)
at android.widget.Spinner.layout(Spinner.java:485)
at android.widget.Spinner.onLayout(Spinner.java:449)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
at android.view.View.layout(View.java:13754)
...
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

以下是我将这些项目放在列表中的方法.

List<MyObject> itemsList = getItemsFromMySource();
if (itemsList!=null && itemsList.size() > 0) {
    CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemsList);
    mySpinner.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)

我的CustomListAdapter使用微调器的默认布局.

public class CustomListAdapter extends ArrayAdapter<MyObject> {

    public CustomListAdapter (Context context, List<MyObject> listItems) {
        super(context, android.R.layout.simple_spinner_item, listItems);
        setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }


    @Override
    public View getView(final int item, View convertView, final ViewGroup parent) {
        final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);

        final TextView text = (TextView) convertView.findViewById(android.R.id.text1);
        text.setText(getItem(item).getItemName());

        return convertView;
    }

    @Override
    public View getDropDownView(final int item, View convertView, final ViewGroup parent) {
        final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);

        final TextView text = (TextView) convertView.findViewById(android.R.id.text1);
        text.setText(getItem(item).getItemName());

        return convertView;
    }
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感谢.

zac*_*ach 6

切勿将微调器适配器设置为null!

mySpinner.setAdapter(null);
Run Code Online (Sandbox Code Playgroud)

我这样做是因为我在我的应用程序功能中需要它.所以我过去常常mySpinner.setEnabled(false)妥协.