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.java
class 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)