这个问题似乎被问了很多,但我找不到答案。我欢迎任何指向可以解决我的问题的副本的链接。
我有两个视图,一个自定义视图,一个在 xml 中定义为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/button_bar_id" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_undo"></Button>
</LinearLayout>
在我的主要活动中,我获得了对这两个视图的引用,并创建了一个 FrameLayout 来将它们放入:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvasView = new CustomCanvasView(this);
buttonBar = (LinearLayout) findViewById(R.id.button_bar_id);
frameLayout = new FrameLayout(this);
frameLayout.addView(canvasView);
frameLayout.addView(buttonBar);
setContentView(frameLayout);
}
就是这样!如果我只使用 FrameLayout 和我的自定义视图,那么一切正常,但是如果我尝试添加预定义的视图 buttonBar,那么我会从初始化它的行中得到一个 nullPointerException。
我查看了这个问题,解决方案似乎是首先调用setContentView(frameLayout)
,但这对我不起作用,实际上使工作案例(我不添加 buttonBar)自相矛盾地崩溃。
关于这里发生了什么的任何想法?
谢谢!
更新
所以,正如一些人建议的那样,我尝试修改我的 xml 文件,以便它包含对我的自定义视图的引用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/button_bar_id" >
<com.collin.customcanvasengine.CustomCanvasView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/customCanvasView">
</com.collin.customcanvasengine.CustomCanvasView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_undo"></Button>
</LinearLayout>
以及修改我的主要活动,以便它使用这个 xml 文件作为它的内容视图:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_bar);
canvasView = (CustomCanvasView) findViewById(R.id.customCanvasView);
canvasView.setDimensions(10,10);
canvasView.requestFocus();
}
现在我得到的错误是我的自定义视图不能被夸大:
android.view.InflateException: Binary XML file line #8: Error inflating class com.collin.customcanvasengine.CustomCanvasView
有任何想法吗?谢谢。
更新
这是 logcat 输出:
06-06 01:55:30.010: E/Trace(19901): error opening trace file: Permission denied (13)
06-06 01:55:30.010: D/ActivityThread(19901): setTargetHeapUtilization:0.25
06-06 01:55:30.010: D/ActivityThread(19901): setTargetHeapIdealFree:8388608
06-06 01:55:30.010: D/ActivityThread(19901): setTargetHeapConcurrentStart:2097152
06-06 01:55:30.050: D/AndroidRuntime(19901): Shutting down VM
06-06 01:55:30.050: W/dalvikvm(19901): threadid=1: thread exiting with uncaught exception (group=0x410b5438)
06-06 01:55:30.060: I/Process(19901): Sending signal. PID: 19901 SIG: 9
06-06 01:55:30.060: E/AndroidRuntime(19901): FATAL EXCEPTION: main
06-06 01:55:30.060: E/AndroidRuntime(19901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.collin.customcanvasengine/com.collin.customcanvasengine.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.collin.customcanvasengine.CustomCanvasView
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2117)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2155)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.ActivityThread.access$700(ActivityThread.java:139)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.os.Looper.loop(Looper.java:137)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.ActivityThread.main(ActivityThread.java:5062)
06-06 01:55:30.060: E/AndroidRuntime(19901): at java.lang.reflect.Method.invokeNative(Native Method)
06-06 01:55:30.060: E/AndroidRuntime(19901): at java.lang.reflect.Method.invoke(Method.java:511)
06-06 01:55:30.060: E/AndroidRuntime(19901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-06 01:55:30.060: E/AndroidRuntime(19901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
06-06 01:55:30.060: E/AndroidRuntime(19901): at dalvik.system.NativeStart.main(Native Method)
06-06 01:55:30.060: E/AndroidRuntime(19901): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.collin.customcanvasengine.CustomCanvasView
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.view.LayoutInflater.createView(LayoutInflater.java:596)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-06 01:55:30.060: E/AndroidRuntime(19901): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.Activity.setContentView(Activity.java:1893)
06-06 01:55:30.060: E/AndroidRuntime(19901): at com.collin.customcanvasengine.MainActivity.onCreate(MainActivity.java:23)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.Activity.performCreate(Activity.java:5058)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
06-06 01:55:30.060: E/AndroidRuntime(19901): ... 11 more
06-06 01:55:30.060: E/AndroidRuntime(19901): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
06-06 01:55:30.060: E/AndroidRuntime(19901): at java.lang.Class.getConstructorOrMethod(Class.java:460)
06-06 01:55:30.060: E/AndroidRuntime(19901): at java.lang.Class.getConstructor(Class.java:431)
06-06 01:55:30.060: E/AndroidRuntime(19901): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
06-06 01:55:30.060: E/AndroidRuntime(19901): ... 22 more
此外,CustomCanvasView 的构造函数的形式为
public CustomCanvasView(Context context)
最后更新
所以克里斯托弗佩里的回答最适合我。正如建议的那样,有必要有一个表单的构造函数,public CustomCanvasView(Context context, AttributeSet attr)
并且重要的是,在这个构造函数中的对 super 的调用中将属性集传递给父级。目前我还没有使用构造函数中设置的属性,但我可能决定将来实现自定义属性。无论哪种方式,它都会起作用。
再次感谢大家,感谢 Christopher Perry 提供的帮助。