Reputation: 88
Here are the code snippet where I receive this error:
final PatchedSpinner spinner = (PatchedSpinner) findViewById(R.id.ActivitySpinner);
Here is the xml:
<Spinner
android:id="@+id/ActivitySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="33dp" />
And also in the activity class I implement this:
public class PatchedSpinner extends Spinner {
public PatchedSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public PatchedSpinner(Context context) {
super(context);
}
public PatchedSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean performClick() {
// this line removed, we do not want to delegate the click to the spinner.
// boolean handled = super.performClick();
Context context = getContext();
final DropDownAdapter adapter = new DropDownAdapter(getAdapter());
CharSequence mPrompt = getPrompt();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (mPrompt != null) {
builder.setTitle(mPrompt);
}
builder.setSingleChoiceItems(adapter, getSelectedItemPosition(), this).show();
return true;
}
}
What am I doing wrong? I cast spinner to PatchedSpinner which extends Spinner...
The stacktrace:
05-05 23:56:11.149: E/AndroidRuntime(368): FATAL EXCEPTION: main
05-05 23:56:11.149: E/AndroidRuntime(368): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.biohouse/com.example.biohouse.CalculateWatt}: android.view.InflateException: Binary XML file line #74: Error inflating class com.example.biohouse.PatchedSpinner
05-05 23:56:11.149: E/AndroidRuntime(368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.os.Looper.loop(Looper.java:123)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-05 23:56:11.149: E/AndroidRuntime(368): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 23:56:11.149: E/AndroidRuntime(368): at java.lang.reflect.Method.invoke(Method.java:521)
05-05 23:56:11.149: E/AndroidRuntime(368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-05 23:56:11.149: E/AndroidRuntime(368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-05 23:56:11.149: E/AndroidRuntime(368): at dalvik.system.NativeStart.main(Native Method)
05-05 23:56:11.149: E/AndroidRuntime(368): Caused by: android.view.InflateException: Binary XML file line #74: Error inflating class com.example.biohouse.PatchedSpinner
05-05 23:56:11.149: E/AndroidRuntime(368): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:128)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
05-05 23:56:11.149: E/AndroidRuntime(368): at com.example.biohouse.CalculateWatt.onCreate(CalculateWatt.java:41)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-05 23:56:11.149: E/AndroidRuntime(368): ... 11 more
05-05 23:56:11.149: E/AndroidRuntime(368): Caused by: java.lang.ClassNotFoundException: com.example.biohouse.PatchedSpinner in loader dalvik.system.PathClassLoader[/data/app/com.example.biohouse-1.apk]
05-05 23:56:11.149: E/AndroidRuntime(368): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-05 23:56:11.149: E/AndroidRuntime(368): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-05 23:56:11.149: E/AndroidRuntime(368): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
05-05 23:56:11.149: E/AndroidRuntime(368): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
05-05 23:56:11.149: E/AndroidRuntime(368): ... 20 more
Upvotes: 0
Views: 241
Reputation: 11988
I think you need to call the related Class name as com.package.name.CustomWidget
instead of the default widget Spinner
in your view as follows:
<com.package.name.PatchedSpinner
android:id="@+id/ActivitySpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="33dp" />
If this is not mentioned, the activity tries to find the default android.widget.Spinner
whereas you declared this with PatchedSpinner
. It might be the cause of the ClassCastException.
Also, make sure the custom class has the right constructors as:
public class PatchedSpinner extends Spinner {
public PatchedSpinner(Context context) {
super(context);
}
public PatchedSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PatchedSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
Upvotes: 2