我在MainActivity中开始布局,每次都出现同样的错误。我的MainActivity,我的SurfaceView是MainActivity中的内部类:
super.onCreate(savedInstanceState);
setContentView(R.layout.backlayout);
backlayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.drawingapp.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="TEST-IT"
android:textStyle="bold" />
<view class = "com.example.drawingapp.MainActivity$DrawingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
DrawingPanel:
public class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
private DrawingThread _thread;
private Path path;
public DrawingPanel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new DrawingThread(getHolder(), this);
}
public DrawingPanel(Context context, AttributeSet atr) {
super(context, atr);
getHolder().addCallback(this);
_thread = new DrawingThread(getHolder(), this);
}
... some crazy stuff
}
错误日志:
05-12 12:41:19.702: E/AndroidRuntime(6313): FATAL EXCEPTION: main
05-12 12:41:19.702: E/AndroidRuntime(6313): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawingapp/com.example.drawingapp.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class com.example.drawingapp.MainActivity$DrawingPanel
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.ActivityThread.access$600(ActivityThread.java:139)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.os.Looper.loop(Looper.java:154)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.ActivityThread.main(ActivityThread.java:4945)
05-12 12:41:19.702: E/AndroidRuntime(6313): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 12:41:19.702: E/AndroidRuntime(6313): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 12:41:19.702: E/AndroidRuntime(6313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-12 12:41:19.702: E/AndroidRuntime(6313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-12 12:41:19.702: E/AndroidRuntime(6313): at dalvik.system.NativeStart.main(Native Method)
05-12 12:41:19.702: E/AndroidRuntime(6313): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class com.example.drawingapp.MainActivity$DrawingPanel
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.view.LayoutInflater.createView(LayoutInflater.java:589)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-12 12:41:19.702: E/AndroidRuntime(6313): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:269)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.Activity.setContentView(Activity.java:1885)
05-12 12:41:19.702: E/AndroidRuntime(6313): at com.example.drawingapp.MainActivity.onCreate(MainActivity.java:47)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.Activity.performCreate(Activity.java:4531)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
05-12 12:41:19.702: E/AndroidRuntime(6313): ... 11 more
05-12 12:41:19.702: E/AndroidRuntime(6313): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
05-12 12:41:19.702: E/AndroidRuntime(6313): at java.lang.Class.getConstructorOrMethod(Class.java:460)
05-12 12:41:19.702: E/AndroidRuntime(6313): at java.lang.Class.getConstructor(Class.java:431)
05-12 12:41:19.702: E/AndroidRuntime(6313): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
05-12 12:41:19.702: E/AndroidRuntime(6313): ... 22 more
我尝试了许多不同的方法来改变XML文件的这一部分,例如:
<view class = "com.example.drawingapp.MainActivity$DrawingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
到
<com.example.drawingapp.MainActivity.DrawingPanel
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
答案 0 :(得分:3)
您的DrawingPanel
是MainActivity
中的内部类,因此必须将其声明为static
,以便可以在没有外部类对象的情况下构建它。
答案 1 :(得分:0)
试试如下:
<com.example.drawingapp.DrawingPanel
android:layout_width="fill_parent"
android:layout_height="fill_parent" />