gpt4 book ai didi

android - 带有自定义 View 类的 InflateException

转载 作者:行者123 更新时间:2023-12-01 16:18:59 25 4
gpt4 key购买 nike

我想简单地在 ImageView 上绘制一个矩形。这是 ImageViewHighlight 类:

public class ImageViewHighlight extends ImageView {
Paint paint = new Paint();

public ImageViewHighlight(Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.BLUE);
canvas.drawRect(30, 20, 60, 40, paint);
}

}

主 Activity :

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

XML:

<com.example.highlight.ImageViewHighlight
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p5" />

但是它崩溃了并给了我这个 logcat:

09-10 15:23:05.320: E/AndroidRuntime(2347): FATAL EXCEPTION: main
09-10 15:23:05.320: E/AndroidRuntime(2347): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.highlight/com.example.highlight.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.ActivityThread.access$600(ActivityThread.java:132)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.os.Looper.loop(Looper.java:137)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.ActivityThread.main(ActivityThread.java:4575)
09-10 15:23:05.320: E/AndroidRuntime(2347): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 15:23:05.320: E/AndroidRuntime(2347): at java.lang.reflect.Method.invoke(Method.java:511)
09-10 15:23:05.320: E/AndroidRuntime(2347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-10 15:23:05.320: E/AndroidRuntime(2347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 15:23:05.320: E/AndroidRuntime(2347): at dalvik.system.NativeStart.main(Native Method)
09-10 15:23:05.320: E/AndroidRuntime(2347): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.view.LayoutInflater.createView(LayoutInflater.java:589)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-10 15:23:05.320: E/AndroidRuntime(2347): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.Activity.setContentView(Activity.java:1835)
09-10 15:23:05.320: E/AndroidRuntime(2347): at com.example.highlight.MainActivity.onCreate(MainActivity.java:12)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.Activity.performCreate(Activity.java:4465)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
09-10 15:23:05.320: E/AndroidRuntime(2347): ... 11 more
09-10 15:23:05.320: E/AndroidRuntime(2347): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
09-10 15:23:05.320: E/AndroidRuntime(2347): at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-10 15:23:05.320: E/AndroidRuntime(2347): at java.lang.Class.getConstructor(Class.java:431)
09-10 15:23:05.320: E/AndroidRuntime(2347): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
09-10 15:23:05.320: E/AndroidRuntime(2347): ... 22 more

我该如何解决这个问题?

最佳答案

InflateException: Binary XML file line #6: Error inflating class com.example.highlight.ImageViewHighlight表示错误出现在 ImageViewHighlight 中。

NoSuchMethodException表示缺少一个方法。

<init> [class android.content.Context, interface android.util.AttributeSet]表示缺少的方法是一个以 Context 和 AttributeSet 作为参数的构造函数。

=> 您需要通过使用相同的参数调用 super() 来实现此方法。

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

关于android - 带有自定义 View 类的 InflateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62320586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com