扩展Spinner以添加索引,错误膨胀类

时间:2013-11-22 17:25:39

标签: android spinner layout-inflater classnotfound

我需要向Spinner添加两个索引,所以我创建了一个Spin extends Spinner类。现在在XML文件中,我使用Spin类的完整路径名,但它给了我“没有在路径上找到类”和“错误膨胀类”错误。 这是代码:

public class AcceptTimetable extends Activity
{
class Spin extends Spinner
{
    int i, j;
    public Spin(Context context) 
    {
        super(context);
    }

    public Spin(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
    }

    public Spin(Context context, int mode) 
    {
        super(context, mode);
        // TODO Auto-generated constructor stub
    }

    public Spin(Context context, AttributeSet attrs, int defStyle, int mode) 
    {
        super(context, attrs, defStyle, mode);
        // TODO Auto-generated constructor stub
    }

    public Spin(Context context, AttributeSet attrs, int defStyle) 
    {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public void setIndices(int a, int b)
    {
        i=a;
        j=b;
    }

    public int getRow()
    {   return i;   }

    public int getColumn()
    {   return j;   }
}

Spin hours[][]=new Spin[6][6];

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.accept_timetable);

    hours[0][0]=(Spin)findViewById(R.id.ttmon1); 
//error at this line


}

这是XML(只是相关部分):

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AcceptTimeTable" >

<TextView
    android:id="@+id/entertt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Enter the time table:" />

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/entertt" >"

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="15dp"
    android:layout_below="@id/entertt" >

    <TableRow
        android:id="@+id/monday"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Mon:" />

        <AcceptTimetable.Spin
            android:id="@+id/ttmon1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"/>

包是“com.example.bunkmaster”,而不是使用“AccpetTimetable.Spin”,即使我说“com.example.bunkmaster.AcceptTimetable.Spin”,它也会给出相同的错误。只是“旋转”也会产生同样的错误。我是Android新手,我无法理解这一点,请帮忙。

当我启动AcceptTimetable活动时,这是堆栈跟踪:

11-22 16:51:02.474: E/AndroidRuntime(2562): FATAL EXCEPTION: main
11-22 16:51:02.474: E/AndroidRuntime(2562): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.example.bunkmaster/com.example.bunkmaster.AcceptTimetable}:
android.view.InflateException: Binary XML file line #39: Error inflating class
AcceptTimetable.Spin 11-22 16:51:02.474: E/AndroidRuntime(2562):    at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
android.app.ActivityThread.access$600(ActivityThread.java:141)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
android.os.Handler.dispatchMessage(Handler.java:99)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
android.os.Looper.loop(Looper.java:137)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
android.app.ActivityThread.main(ActivityThread.java:5041)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
java.lang.reflect.Method.invokeNative(Native Method)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
java.lang.reflect.Method.invoke(Method.java:511)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at dalvik.system.NativeStart.main(Native Method)
11-22 16:51:02.474: E/AndroidRuntime(2562): Caused by: android.view.InflateException: Binary XML file line #39: Error inflating class AcceptTimetable.Spin
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at com.example.bunkmaster.AcceptTimetable.onCreate(AcceptTimetable.java:66)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.app.Activity.performCreate(Activity.java:5104)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-22 16:51:02.474: E/AndroidRuntime(2562):     ... 11 more
11-22 16:51:02.474: E/AndroidRuntime(2562): Caused by: java.lang.ClassNotFoundException: Didn't find class "AcceptTimetable.Spin" on path: /data/app/com.example.bunkmaster-2.apk
11-22 16:51:02.474: E/AndroidRuntime(2562):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
11-22 16:51:02.474: E/AndroidRuntime(2562):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
11-22 16:51:02.474: E/AndroidRuntime(2562):     ... 22 more

1 个答案:

答案 0 :(得分:1)

我明白了,伙计们。 我在自己的java文件中定义了Spin类,在XML中我使用了“com.example.bunkmaster.Spin”。工作就像一个魅力^ _ ^