android.content.res.Resources $ NotFoundException:Spinner中的资源ID#0x0

时间:2017-01-16 15:57:14

标签: java android

所以这个错误不断出现。当我打开微调器时会发生这种情况。微调器在FragmentDialog中,如果它可以告诉你关于错误的任何信息。

android.content.res.Resources$NotFoundException: Resource ID #0x0
                  at android.content.res.Resources.getValue(Resources.java:1351)
                  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2774)
                  at android.content.res.Resources.getLayout(Resources.java:1165)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
                  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:378)
                  at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:448)
                  at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:963)
                  at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:959)
                  at android.widget.Spinner.measureContentWidth(Spinner.java:833)
                  at android.widget.Spinner$DropdownPopup.computeContentWidth(Spinner.java:1194)
                  at android.widget.Spinner$DropdownPopup.show(Spinner.java:1220)
                  at android.widget.Spinner.performClick(Spinner.java:758)
                  at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:438)
                  at android.view.View$PerformClick.run(View.java:21147)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我现在一直在寻找问题的根源两个小时。

此处适配器适用的addMethod.java

public class addMethod extends DialogFragment {
boolean chooseAType = false;
ArrayList<PaymentMethod> paymentMethods = new ArrayList<>();
Context context;
String[] names;
public addMethod newInstance(Context context) {


    addMethod fragment = new addMethod();
    fragment.context =context;
    return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.add_method_fragment, container);
    getDialog().setTitle("Add payment method.");
    final Spinner spinner = (Spinner) view.findViewById(R.id.methodSpinner);
    names = getResources().getStringArray(R.array.PaymentTypes);
    for(int i = 0;i < names.length;i++){

        paymentMethods.add(new PaymentMethod(names[i],i));

    }
    paymentMethods.add(0,new PaymentMethod("Choose A Country",-1));

    MethodTypesAdapter methodTypesAdapter = new      MethodTypesAdapter(getActivity(),0,paymentMethods);
    spinner.setAdapter(methodTypesAdapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            chooseAType = true;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    final TextInputLayout editText = (TextInputLayout) view.findViewById(R.id.addNameInput);
    TextView ok = (TextView) view.findViewById(R.id.okButton);
    final int pos = spinner.getSelectedItemPosition();
    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddMethodFeedback addMethodFeedback = (AddMethodFeedback) getActivity();
            if (editText.getEditText().getText().toString().trim().length() != 0 && !chooseAType) {
                getDialog().dismiss();
                addMethodFeedback.methodName(editText.getEditText().toString(), pos);
            }
        }
    });
    TextView cancel = (TextView) view.findViewById(R.id.cancelButton);
    cancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getDialog().dismiss();
        }
    });
    getDialog().setTitle(getTag());

    return view;
}
}

Heres MethodTypesAdapter.java上课。

public class MethodTypesAdapter extends ArrayAdapter<PaymentMethod> {
public MethodTypesAdapter(Context context, int resource, ArrayList<PaymentMethod> objects) {
    super(context, 0, objects);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    PaymentMethod paymentMethod = getItem(position);
    Utils utils = new Utils(null);

    if (paymentMethod.getType() == -1) {
        convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, null, false);
        TextView name = (TextView) convertView.findViewById(android.R.id.text1);
        name.setText(getContext().getResources().getString(R.string.chooseAMethod));
    } else {

        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_with_pic, null, false);
        TextView name = (TextView) convertView.findViewById(R.id.itemsName);
        name.setText(paymentMethod.getName());
        ImageView icon = (ImageView) convertView.findViewById(R.id.itemsIcon);
        icon.setImageResource(utils.methodTypeIcons()[position]);
    }


    return convertView;
}
}

2 个答案:

答案 0 :(得分:5)

如果您要使用Spinner,则需要call setDropDownViewResource() on your ArrayAdapter(或类似MethodTypesAdapter的子类),以提供在微调器打开并显示时使用的布局资源滚动列表供用户选择。因此,例如,您可以在android.R.layout.simple_spinner_item构造函数和MethodTypesAdapter中使用getView(),并将android.R.layout.simple_spinner_dropdown_itemsetDropDownViewResource()一起使用。

答案 1 :(得分:2)

添加到CommonsWare的答案:

或者,如果您使用自己的自定义ArrayAdapter和Spinner,则需要实现/覆盖getDropDownView(...)方法以及通常的getView(...)方法。