4

I have a custom ImageView Class as below

public class MyImageView extends ImageView
{

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

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvas.drawLine(0, 0, 20, 20, p);
    super.onDraw(canvas);
}
}

and inside my Activity class the Oncreate methode is defined as

MyImageView imageView;
      // It works perfect when i use ImageView instead of MyImageView 
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView=(MyImageView)findViewById(R.id.image);
    Bitmap dbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dinkan);
    Bitmap bitmap = dbitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    imageView.setImageBitmap(bitmap);
}

It ends with an RuntimeException caused by InflateException. The error log is shown below

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hari.myapplication/com.example.hari.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class com.example.hari.myapplication.MyImageView
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
          at android.app.ActivityThread.access$800(ActivityThread.java:151)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
          at android.os.Handler.dispatchMessage(Handler.java:110)
          at android.os.Looper.loop(Looper.java:193)
          at android.app.ActivityThread.main(ActivityThread.java:5299)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:515)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
          at dalvik.system.NativeStart.main(Native Method)
       Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.example.hari.myapplication.MyImageView
          at android.view.LayoutInflater.createView(LayoutInflater.java:603)
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
          at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
          at com.example.hari.myapplication.MainActivity.onCreate(MainActivity.java:29)
          at android.app.Activity.performCreate(Activity.java:5264)
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
          at android.app.ActivityThread.access$800(ActivityThread.java:151) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
          at android.os.Handler.dispatchMessage(Handler.java:110) 
          at android.os.Looper.loop(Looper.java:193) 
          at android.app.ActivityThread.main(ActivityThread.java:5299) 
          at java.lang.reflect.Method.invokeNative(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:515) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645) 
          at dalvik.system.NativeStart.main(Native Method) 
       Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
          at java.lang.Class.getConstructorOrMethod(Class.java:472)
          at java.lang.Class.getConstructor(Class.java:446)
          at android.view.LayoutInflater.createView(LayoutInflater.java:568)
          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
          at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
          at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
          at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 
          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
          at com.example.hari.myapplication.MainActivity.onCreate(MainActivity.java:29) 
          at android.app.Activity.performCreate(Activity.java:5264) 
          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302) 
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
          at android.app.ActivityThread.access$800(ActivityThread.java:151) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
          at android.os.Handler.dispatchMessage(Handler.java:110) 
          at android.os.Looper.loop(Looper.java:193) 
          at android.app.ActivityThread.main(ActivityThread.java:5299) 
          at java.lang.reflect.Method.invokeNative(Native Method

But it works perfect when i use imageview instead of a custom ImageView(MyImageView). So what is wrong with my code? Any help is appreciated.

0

1 Answer 1

6

When inflating a custom View from XML, you must have all the correct constructors. try adding these constructors as well as the one you have

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

public MyImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
Sign up to request clarification or add additional context in comments.

1 Comment

It was the problem .Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.