这是错误,似乎指向在 DrawView 中定义的 init() 函数中的空指针。我正在尝试显示在 onDraw() 上绘制的一些形状,并在其下方显示一个按钮。
致命异常:主要 android.view.InflateException:二进制 XML 文件第 7 行:膨胀类错误
com.example.canvas_shapes2.DrawView
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:269)
at android.app.Activity.setContentView(Activity.java:1885)
at com.example.canvas_shapes2.MainGameScreen$1.onClick(MainGameScreen.java:31)
at android.view.View.performClick(View.java:3549)
at android.view.View$PerformClick.run(View.java:14400)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4944)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:586)
... 19 more
Caused by: java.lang.NullPointerException
at com.example.canvas_shapes2.DrawView.init(DrawView.java:271)
at com.example.canvas_shapes2.DrawView.<init>(DrawView.java:254)
game_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view class="com.example.canvas_shapes2.DrawView"
android:id="@+id/dview"
android:layout_width="wrap_content"
android:layout_height="370dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/display_challengeX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/display_challenge_button"
/>
</LinearLayout>
</LinearLayout>
public class MainGameScreen extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start_game0).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new DialogBox();
newFragment.show(getSupportFragmentManager(), "chall_diag");
setContentView(R.layout.game_screen);
}
});
public class DrawView extends View {
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context ctx) {
Button button = (Button) findViewById(R.id.display_challengeX);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("ABC");
}
});
}