无法在使用xml扩展视图的类中嵌套按钮

时间:2013-07-13 22:09:00

标签: android xml button view

这是错误,似乎指向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");
        }
    });

}

1 个答案:

答案 0 :(得分:0)

想出来,不得不搬家:

Button button = (Button) findViewById(R.id.display_challengeX);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        System.out.println("ABC");
    }
});

来自DrawView的init(),并在将game_screen.xml设置为内容视图setContentView(R.layout.game_screen)之后在MainGameScreen.java中定义它;

public class MainGameScreen extends FragmentActivity {
Bundle args = new Bundle();

@Override
public 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);

            final Button button = (Button) findViewById(R.id.display_challengeX);
            button.setOnClickListener(new View.OnClickListener() {

                DialogFragment newFragment = new DialogBox();
                @Override
                public void onClick(View v) {
                    newFragment.show(getSupportFragmentManager(), "chall_diag");
                }
            });

        }

    });