Reputation: 3437
I'm making an example that rotate an image like this post.
I've extends ImageView then override the OnDraw and put in MainActivity. But It fired "InflateException".
Here is my example code:
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();
}
}
MainActivity
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>
Manifest
<?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>
Logcat
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
Did I do something wrong?
Upvotes: 1
Views: 2135
Reputation: 8478
You're missing the constructor for MImageView(Context, Attributeset). Just add it and the error will be gone :)
Upvotes: 1
Reputation: 2174
You have not implemented constructor with (class android.content.Context, interface android.util.AttributeSet) signature.
Upvotes: 6