2012-10-16 90 views
0

我想我的自定义ImageView添加到XML布局。Android的自定义组件布局

的main.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" > 

    <com.android.gag.TouchImageView 
     android:id="@+id/Image1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:adjustViewBounds="true" 
     android:clickable="true" 
     android:scaleType="center" /> 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" /> 

</RelativeLayout> 

TouchImageView,正如它的名字所暗示的,extendsImageView类。

Main.java

touchImageView = (TouchImageView)findViewById(R.id.Image1);

我的应用程序崩溃。 logcat的输出:

10-16 20:38:20.275: E/AndroidRuntime(11354): FATAL EXCEPTION: main 
10-16 20:38:20.275: E/AndroidRuntime(11354): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gag/com.android.gag.Main}: android.view.InflateException: Binary XML file line #6: Error inflating class com.android.gag.TouchImageView 
10-16 20:38:20.275: E/AndroidRuntime(11354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970) 
idRuntime(11354): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.android.gag.TouchImageView 
10-16 20:38:20.275: E/AndroidRuntime(11354): at android.view.LayoutInflater.createView(LayoutInflater.java:589) 
10-16 20:38:20.275: E/AndroidRuntime(11354): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 

请,任何人都可以帮助我吗? 我是初学者,所以像'replace "class library" with "Android library project"'这样的答案是毫无意义的,因为它们太模糊,我不知道从哪里开始以及去哪里。

编辑:链接到我的TouchImageView class

+1

请你说明你的TouchImageView的实现。 – Daniel

+0

https://dl.dropbox.com/u/43107774/TouchImageView.java – Teo

+0

当您错误地重写ImageView的方法时,通常会发生此问题。 – Daniel

回答

2

代码为您的自定义视图类缺少两个构造函数。从Android docs for the View class

View(Context context, AttributeSet attrs) 

构造充气从XML的视图时调用。

View(Context context, AttributeSet attrs, int defStyle) 

从XML执行通货膨胀并应用特定于类的基本样式。

它崩溃,因为它无法找到它所需要的构造函数,所以它不能膨胀的观点。

您TouchImageView类实现这两个构造,看看问题是否会消失。