在膨胀自定义视图时出现Android错误

时间:2014-08-07 21:10:12

标签: android xml eclipse android-layout android-custom-view

我正在尝试使用此插件显示可缩放的图像。

https://github.com/MikeOrtiz/TouchImageView

我已经在eclipse中导入了它,并在我的项目属性中引用它。这是我的xml:

自定义视图为com.example.touch.TouchImageView

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
   style="@style/RegularView"  
   android:orientation="vertical" >

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <com.example.touch.TouchImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ImageButton
            android:id="@+id/prev_button"
            android:contentDescription="@string/fish_cd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_action_previous_item" />

        <ImageButton
            android:id="@+id/next_button"
            android:contentDescription="@string/fish_cd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_action_next_item" />

    </RelativeLayout>


    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >

        <TextView
            android:id="@+id/comment"
            android:layout_margin="5dp"
            android:singleLine="true" 
            android:ellipsize="end"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal" >

            <TextView
                android:id="@+id/date"
                android:layout_gravity="center_horizontal"
                android:layout_margin="5dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>


            <TextView
                android:id="@+id/page"
                android:layout_margin="5dp"
                android:layout_gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
             android:id="@+id/admin_controls"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal" >

             <CheckBox
                 android:id="@+id/switch1"
                 android:layout_gravity="center"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="@string/main" />

             <CheckBox
                 android:id="@+id/switch2"
                 android:layout_gravity="center"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="@string/approved" />

             <Button
                 android:id="@+id/delete_button"
                 android:layout_gravity="center"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
                 android:layout_weight="1"
                 android:text="@string/delete"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

当我开始活动时,它会崩溃:

08-07 17:07:09.340: E/AndroidRuntime(2106): FATAL EXCEPTION: main
08-07 17:07:09.340: E/AndroidRuntime(2106): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.arin/android.arin.GalleryScreen}: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.touch.TouchImageView
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.ActivityThread.access$700(ActivityThread.java:143)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.os.Looper.loop(Looper.java:137)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.ActivityThread.main(ActivityThread.java:4950)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at java.lang.reflect.Method.invoke(Method.java:511)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at dalvik.system.NativeStart.main(Native Method)
08-07 17:07:09.340: E/AndroidRuntime(2106): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.touch.TouchImageView
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.Activity.setContentView(Activity.java:1915)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.arin.GalleryScreen.onCreate(GalleryScreen.java:44)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.Activity.performCreate(Activity.java:5177)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-07 17:07:09.340: E/AndroidRuntime(2106):     ... 11 more
08-07 17:07:09.340: E/AndroidRuntime(2106): Caused by: java.lang.ClassNotFoundException: com.example.touch.TouchImageView
08-07 17:07:09.340: E/AndroidRuntime(2106):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
08-07 17:07:09.340: E/AndroidRuntime(2106):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
08-07 17:07:09.340: E/AndroidRuntime(2106):     ... 22 more

有谁知道这里有什么不对吗?

由于

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

尝试运行一个干净的解决方案,然后再试一次我以前见过这么多次,通常是一个参考问题,或者xml文件本身的一些问题..即:非关闭匹配标签。等。

它说无法找到类,尝试重新启动eclipse和/或再次检查引用。

答案 1 :(得分:0)

我认为问题出在TouchImageView路径上。

如果您的CustomView在src包中包含文件,则使用该路径。

例如,假设您的包名是com.example.touch.someapp,如果TouchImageView文件在此包中,则使用如下所示。

 <com.example.touch.someapp.TouchImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

但是,如果要链接项目,那么它应该是CustomView存在的确切路径。

对于您的示例,如果您从

链接项目

https://github.com/MikeOrtiz/TouchImageView,那么您的观点应如下所示

 <com.ortiz.touch.TouchImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

如果有效,请告诉我。