ResourceNotFoundException虽然有资源

时间:2016-01-21 06:31:40

标签: android spinner android-adapter

我正在尝试创建一个带有不可选文本的微调器,当下拉列表不显示为“--select option--”时。我使用SO post的第二个答案为“TextView”创建了一个自定义微调器适配器和一个--select option--的布局文件。

虽然我已经传递了构造函数的布局文件,但我得到了6912-6912/com.examp.three E/AndroidRuntime﹕ FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: Resource ID #0x7f04004e这个异常。

我做错了什么?

我刚调试了应用,getNothingSelectedLayout的值为2130968654。我把断点保持在return layoutInflater.inflate(nothingSelectedLayout, parent, false);getNothingSelectedLayout的值仍然相同。在那之后,当我按下F8时,它会在崩溃之前转到Choreographer.class

就像@Kumar Ranjan在他的回答评论中所说,我检查了R.java类的资源ID,错误日志说不存在。但我在support_simple_spinner_dropdown_item文件中找到了分配给R.java的ID。我在Resource ID #0x7f04004e文件

中找到了错误日志中显示的R.java

以下是代码:

    ArrayAdapter<String> adapterBankName = new ArrayAdapter
            (CheckAmountActivity.this, android.R.layout.simple_spinner_dropdown_item, paymentMode) {
        public View getDropDownView(int position, View convertView, ViewGroup parent) {

            View v = super.getDropDownView(position, convertView, parent);
            ((TextView) v).setGravity(Gravity.CENTER);
            ((TextView) v).setTextColor(Color.BLACK);

            return v;

        }
    };

    spinnerBankName.setAdapter(new NDSpinner(adapterBankName, R.layout.spinnertitle, getApplicationContext())); //the constructor of NDSpinner class

这是NDSpinner类:

public class NDSpinner implements SpinnerAdapter, ListAdapter {

protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
String item;


public NDSpinner(
        SpinnerAdapter spinnerAdapter,
        int nothingSelectedLayout, Context context) {

    this(spinnerAdapter, nothingSelectedLayout, -1, context);
}


public NDSpinner(SpinnerAdapter spinnerAdapter,
                 int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
    this.adapter = spinnerAdapter;
    this.context = context;
    this.nothingSelectedLayout = nothingSelectedLayout;
    this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
    layoutInflater = LayoutInflater.from(context);
}

@Override
public final View getView(int position, View convertView, ViewGroup parent) {
    // This provides the View for the Selected Item in the Spinner, not
    // the dropdown (unless dropdownView is not set).
    if (position == 0) {
        return getNothingSelectedView(parent);
    }
    return adapter.getView(position - EXTRA, null, parent); // Could re-use
    // the convertView if possible.
}

/**
 * View to show in Spinner with Nothing Selected
 * Override this to do something dynamic... e.g. "37 Options Found"
 *
 * @param parent
 * @return
 */
protected View getNothingSelectedView(ViewGroup parent) {
    return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    // Android BUG! http://code.google.com/p/android/issues/detail?id=17128 -
    // Spinner does not support multiple view types
    if (position == 0) {
        return nothingSelectedDropdownLayout == -1 ?
                new View(context) :
                getNothingSelectedDropdownView(parent);
    }

    // Could re-use the convertView if possible, use setTag...
    return adapter.getDropDownView(position - EXTRA, null, parent);
}

/**
 * Override this to do something dynamic... For example, "Pick your favorite
 * of these 37".
 *
 * @param parent
 * @return
 */
protected View getNothingSelectedDropdownView(ViewGroup parent) {
    return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
}

@Override
public int getCount() {
    int count = adapter.getCount();
    return count == 0 ? 0 : count + EXTRA;
}

@Override
public Object getItem(int position) {
    return position == 0 ? null : adapter.getItem(position - EXTRA);
}

@Override
public int getItemViewType(int position) {
    return 0;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public long getItemId(int position) {
    return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
}

@Override
public boolean hasStableIds() {
    return adapter.hasStableIds();
}

@Override
public boolean isEmpty() {
    return adapter.isEmpty();
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    adapter.registerDataSetObserver(observer);
}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
    adapter.unregisterDataSetObserver(observer);
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return position != 0; // Don't allow the 'nothing selected'
    // item to be picked.
}

}

这是spinnertitle.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:gravity="center"
    android:background="@drawable/button_custom"
    android:padding="5dp"
    android:layout_margin="5dp"
    android:textSize="18sp"
    android:text="Vehicle Type" />

这是logcat中的错误:

01-21 12:35:10.740    8658-8658/com.examp.three E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x7f04004e
        at android.content.res.Resources.getValue(Resources.java:1033)
        at android.content.res.Resources.loadXmlResourceParser(Resources.java:2179)
        at android.content.res.Resources.getLayout(Resources.java:872)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
        at com.examp.three.adapter.NDSpinner.getNothingSelectedView(NDSpinner.java:59)
        at com.examp.three.adapter.NDSpinner.getView(NDSpinner.java:45)
        at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:194)
        at android.widget.Spinner.onMeasure(Spinner.java:440)
        at android.view.View.measure(View.java:15775)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:15775)
        at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1228)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.widget.ScrollView.onMeasure(ScrollView.java:321)
        at android.view.View.measure(View.java:15775)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4942)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2193)
        at android.view.View.measure(View.java:15775)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2213)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1291)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1486)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1181)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4943)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
        at android.view.Choreographer.doCallbacks(Choreographer.java:579)
        at android.view.Choreographer.doFrame(Choreographer.java:548)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
        at android.os.Handler.handleCallback(Handler.java:800)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5391)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

尝试清理项目并重建它。

答案 1 :(得分:0)

进行以下更改并尝试一次。

    protected View getNothingSelectedView(ViewGroup parent) {
        return layoutInflater.inflate(nothingSelectedLayout, parent, **true**);
    }

答案 2 :(得分:-1)

我可以看到您的资源布局没有父布局它应该看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"

    tools:context=".MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Lo más valioso que tienes es tu tiempo"
        android:textColor="@color/white"
        android:layout_centerHorizontal="true"
        android:textSize="15dp"
        android:layout_marginTop="20dp"/>

</RelativeLayout>