我正在尝试自定义Spinner类,我可以在我的Android应用程序的xml文件中使用它。
我的Andrid应用布局xml的片段:
<mobileapps.android_app.assist.MyCustomSpinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:id="@+id/D3_Spinner_1"
android:entries="@array/minutes"
android:layout_centerInParent="true"
/>
我的自定义微调器类:
package mobileapps.android_app.assist;
import android.content.Context;
import android.graphics.Typeface;
import android.widget.ArrayAdapter;
import mobileapps.android_app.R;
/**
* Custom Spinner Class.
* Erzeugt Typeface
*/
public class MyCustomSpinner extends android.support.v7.widget.AppCompatSpinner {
private ArrayAdapter arrayAdapter = null;
public MyCustomSpinner(Context context) {
super(context);
arrayAdapter = new ArrayAdapter(context, R.layout.spinner_item);
arrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
this.setAdapter(arrayAdapter);
}
}
文件R.layout.spinner_dropdown_item
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee" />
文件R.layout.spinner_item
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
当我在模拟器中运行时,我收到此错误:
04-06 01:50:10.428 15550-15550/mobileapps.android_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: mobileapps.android_app, PID: 15550
java.lang.RuntimeException: Unable to start activity ComponentInfo{mobileapps.android_app/mobileapps.android_app.knowledgeCollectorActivities_2}: android.view.InflateException: Binary XML file line #67: Binary XML file line #67: Error inflating class mobileapps.android_app.assist.MyCustomSpinner
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #67: Binary XML file line #67: Error inflating class mobileapps.android_app.assist.MyCustomSpinner
Caused by: android.view.InflateException: Binary XML file line #67: Error inflating class mobileapps.android_app.assist.MyCustomSpinner
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructor0(Class.java:2204)
at java.lang.Class.getConstructor(Class.java:1683)
at android.view.LayoutInflater.createView(LayoutInflater.java:618)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at mobileapps.android_app.knowledgeCollectorActivities_2.onCreate(knowledgeCollectorActivities_2.java:48)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
有没有人可以帮我解决这个问题? 谢谢, GPclips