-1

I'm trying to create a spinner with a non-selectable text displayed when the dropdown is not displayed like "--select option--". I have created a custom spinner adapter and a layout file with a TextView for the "--select option--" using the second answer of this SO post.

I'm getting 6912-6912/com.examp.three E/AndroidRuntime﹕ FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: Resource ID #0x7f04004e this exception though I have passed a layout file for the constructor.

What am I doing wrong?

I just debugged the app and the value for getNothingSelectedLayout was 2130968654. I kept the break point at the return layoutInflater.inflate(nothingSelectedLayout, parent, false);. The value of getNothingSelectedLayout was still the same. After that point, when I press F8, it goes to Choreographer.class before crashing

Like @Kumar Ranjan said in the comments of his answer, I checked the R.javaclass for the Resource ID which the error log said was not there. But I found the ID assigned to support_simple_spinner_dropdown_item in the R.java file. I found the Resource ID #0x7f04004e shown in the error log in the R.java file

Here's the code:

    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

This is NDSpinner class:

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.
}

}

This is 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" />

This is the error in 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)
Community
  • 1
  • 1
Abhi
  • 1,512
  • 2
  • 22
  • 46
  • what is the name of the layout xml file? – Pooya Jan 21 '16 at 06:36
  • 1
    What folder is that layout in? And which line is throwing the exception? – Mike M. Jan 21 '16 at 06:45
  • 1
    for spinnertitle.xml can you please try changing android:id from ***@android:id/text1*** to ***@+id/text1*** – Rajen Raiyarela Jan 21 '16 at 06:45
  • Do you also have simple_spinner_dropdown_item in the layout folder? – Pooya Jan 21 '16 at 06:46
  • which line throws the error? – Irina Avram Jan 21 '16 at 06:52
  • @Abhi please try what Rajen Raiyarela suggested. you are not generating the id in R.java unless it's starting with + – Sunil Sunny Jan 21 '16 at 06:54
  • @MikeM. it's in the res/layout folder. the error occurs In this method of NDSpinner class: `protected View getNothingSelectedView(ViewGroup parent) { return layoutInflater.inflate(nothingSelectedLayout, parent, false); }` – Abhi Jan 21 '16 at 07:08
  • @RajenRaiyarela that didn't work – Abhi Jan 21 '16 at 07:10
  • 1
    provide whole stack trace – KR_Android Jan 21 '16 at 07:12
  • can you verify from R.java resource id 0x7f04004e belongs to text1 only? – Rajen Raiyarela Jan 21 '16 at 07:18
  • @KumarRanjan I've edited and posted it in the question. I think that's what you meant..sorry I'm new to this – Abhi Jan 21 '16 at 07:18
  • @RajenRaiyarela..sorry, how do I do that? I can't find the R.java class in the project explorer. I'm using Android studio – Abhi Jan 21 '16 at 07:23
  • @IrinaAvram I've posted the entire error log. The error occurs in the `getNothingSelectedView(ViewGroup parent)` method of `NDSpinner` class – Abhi Jan 21 '16 at 07:27
  • @MikeM. I've posted the entire error log in the question – Abhi Jan 21 '16 at 07:31
  • Hmm. Dunno. Does it work if you use `android.R.layout.simple_spinner_item` instead of your layout? – Mike M. Jan 21 '16 at 07:36
  • @MikeM. That didn't work either :( ..And I wonder why my question got downvoted – Abhi Jan 21 '16 at 09:47
  • Yeah, I'm not sure what the problem is. Also, I don't see any downvotes on your question. – Mike M. Jan 21 '16 at 09:51
  • @MikeM.I just debugged the app and the value for `getNothingSelectedLayout` was `2130968654`. I kept the break point at the `return layoutInflater.inflate(nothingSelectedLayout, parent, false);`. The value of `getNothingSelectedLayout` was still the same. After that point, when I press F8, it goes to `Choreographer.class` before crashing. I've edited the comment. It's not getNothingSelectedView. I meant getNothingSelectedLayout – Abhi Jan 21 '16 at 11:02

3 Answers3

2

Try cleaning your project and rebuilding it.

rohit
  • 31
  • 4
0

make below change and try once.

    protected View getNothingSelectedView(ViewGroup parent) {
        return layoutInflater.inflate(nothingSelectedLayout, parent, **true**);
    }
KR_Android
  • 1,149
  • 9
  • 19
  • can you check R.java file? All you need to do is search R.java in your project. Open it and search the vaule 7f04004e – KR_Android Jan 21 '16 at 09:58
  • If you rebuild the project after posting this question, you may need to take a fresh log and check for the new reference in the R.java – KR_Android Jan 21 '16 at 10:00
  • I searched in the file. There was no such value. I just debugged the app and the value for `getNothingSelectedLayout` was `2130968654`. I kept the break point at the `return layoutInflater.inflate(nothingSelectedLayout, parent, false);`. The value of `getNothingSelectedLayout` was still the same. After that point, when I press F8, it goes to `Choreographer.class` before crashing. – Abhi Jan 21 '16 at 11:13
  • The way you have provided the stack trace, take the trace again and check this line android.content.res.Resources$NotFoundException: Resource ID #0x7f04004e. ID (#0x7f04004e) may have changed this time. You need to check new id into R.java – KR_Android Jan 21 '16 at 11:16
  • I got that value #0x7f04004e on running the app again in the error log and also found it in the `R.java` file. It was assigned to `support_simple_spinner_dropdown_item` – Abhi Jan 21 '16 at 11:47
  • what is the targetVersion, minSDKVersion and device api level you are using? – KR_Android Jan 22 '16 at 08:40
-1

Your resource layout as i can see does not have a parent layout It should look like this

<?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>
Pratik
  • 456
  • 4
  • 18
  • I think it's not supposed to have a parent layout. – Abhi Jan 21 '16 at 06:38
  • 1
    No, it doesn't have to have a parent. Have a look at [`simple_spinner_item`](https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_spinner_item.xml). – Mike M. Jan 21 '16 at 06:40
  • you can't do this way..otherwise it will still shows the same error – Pratik Jan 21 '16 at 06:42