1

我正在制作一个旋转像这篇文章这样的图像的例子。
我扩展了 ImageView,然后覆盖 OnDraw 并放入 MainActivity。但它触发了“InflateException”。这是我的示例代码:
MImageView

public class MImageView extends ImageView {

    public MImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.save();
        canvas.rotate(45, 0, 0);
        super.onDraw(canvas);
        canvas.restore();
    }
}

主要活动

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MImageView mImageView = (MImageView) findViewById(R.id.imageView1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

main_activity.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=".MainActivity" >



    <com.example.rotatetest.MImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

显现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.rotatetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.rotatetest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

日志猫

08-16 10:21:31.820: D/AndroidRuntime(5135): Shutting down VM
08-16 10:21:31.820: W/dalvikvm(5135): threadid=1: thread exiting with uncaught exception (group=0x40ab4210)
08-16 10:21:31.820: E/AndroidRuntime(5135): FATAL EXCEPTION: main
08-16 10:21:31.820: E/AndroidRuntime(5135): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rotatetest/com.example.rotatetest.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.example.rotatetest.MImageView
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.ActivityThread.access$600(ActivityThread.java:134)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.os.Looper.loop(Looper.java:137)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.ActivityThread.main(ActivityThread.java:4697)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at java.lang.reflect.Method.invokeNative(Native Method)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at java.lang.reflect.Method.invoke(Method.java:511)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at dalvik.system.NativeStart.main(Native Method)
08-16 10:21:31.820: E/AndroidRuntime(5135): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.example.rotatetest.MImageView
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.view.LayoutInflater.createView(LayoutInflater.java:589)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.Activity.setContentView(Activity.java:1879)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at com.example.rotatetest.MainActivity.onCreate(MainActivity.java:15)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.Activity.performCreate(Activity.java:4539)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2013)
08-16 10:21:31.820: E/AndroidRuntime(5135):     ... 11 more
08-16 10:21:31.820: E/AndroidRuntime(5135): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
08-16 10:21:31.820: E/AndroidRuntime(5135):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at java.lang.Class.getConstructor(Class.java:431)
08-16 10:21:31.820: E/AndroidRuntime(5135):     at android.view.LayoutInflater.createView(LayoutInflater.java:561)
08-16 10:21:31.820: E/AndroidRuntime(5135):     ... 22 more

我做错什么了吗?

4

2 回答 2

6

您尚未使用 (class android.content.Context, interface android.util.AttributeSet) 签名实现构造函数。

于 2013-08-16T02:25:30.153 回答
1

您缺少 MImageView(Context, Attributeset) 的构造函数。只需添加它,错误就会消失:)

于 2013-08-16T02:26:12.820 回答