Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中由按钮导致的空指针异常_Android - Fatal编程技术网

Android中由按钮导致的空指针异常

Android中由按钮导致的空指针异常,android,Android,我的活动课是这样的: public class FullscreenActivity extends AppCompatActivity { private Button btnTakePhoto = new Button(this); private Button btnRecordVideo = new Button(this); @Override protected void onCreate(Bundle savedInstanceState) {

我的活动课是这样的:

public class FullscreenActivity extends AppCompatActivity {
    private Button btnTakePhoto = new Button(this);
    private Button btnRecordVideo = new Button(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);

        btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
        btnTakePhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                helloWorldKamera();
            }
        });
    }

    public void helloWorldKamera() {
        System.out.println("Kamera");
    }

}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:text="Foto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:id="@+id/btnTakePhoto" />

        <Button
            android:text="Video"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:id="@+id/btnRecordVideo"
            />
    </LinearLayout>
</FrameLayout>
我的
activity\u fullscreen.xml
文件如下所示:

public class FullscreenActivity extends AppCompatActivity {
    private Button btnTakePhoto = new Button(this);
    private Button btnRecordVideo = new Button(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);

        btnTakePhoto = (Button) findViewById(R.id.btnTakePhoto);
        btnTakePhoto.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                helloWorldKamera();
            }
        });
    }

    public void helloWorldKamera() {
        System.out.println("Kamera");
    }

}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:text="Foto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:id="@+id/btnTakePhoto" />

        <Button
            android:text="Video"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:id="@+id/btnRecordVideo"
            />
    </LinearLayout>
</FrameLayout>

现在我得到以下错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.view$OnClickListener)”

您知道如何修复空指针异常吗?我不知道为什么我的按钮为空。
我初始化按钮的方式是否正确,或者错误是由
findViewById
引起的?

替换

  private Button btnTakePhoto = new Button(this);


不能将按钮设置为“新建”按钮(此按钮)。如果没有错误,它可能总是空的。我将仅在onCreate方法中定义按钮,然后将其设置为必要的id


为了明确回答您的问题,代码的findViewById部分是正确的。你刚刚搞乱了按钮变量的定义

,但是删除
新按钮(this)
也不起作用。你应该在方法中定义你的按钮。我猜它是一个不同的空指针,由super.onCreate()上方的两行引起