Java Button.findViewById()上的NullPointerException

Java Button.findViewById()上的NullPointerException,java,android,xml,Java,Android,Xml,我按照教程在Android Studio中创建了一个对话框。我的代码在Java文件中没有显示错误,但是说“不幸的是,应用程序已经停止工作”。我不知道为什么 我的java文件: import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.

我按照教程在Android Studio中创建了一个对话框。我的代码在Java文件中没有显示错误,但是说“不幸的是,应用程序已经停止工作”。我不知道为什么

我的java文件:

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private static Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        onButoonListener();
    }

    public void onButoonListener(){
        b1=(Button) b1.findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder ad = new          AlertDialog.Builder(MainActivity.this);
                    ad.setMessage("Do you want to   close").setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).setNegativeButton("no", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                    AlertDialog ad1= ad.create();
                    ad1.setTitle("Alert!!");
                    ad1.show();
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
我的Xml文件:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="188dp" />

</RelativeLayout>

您以错误的方式调用了
findViewById()
方法。它应该在
活动
本身上调用,而不是在
按钮
引用上调用。替换

b1=(Button) b1.findViewById(R.id.button);


b1
引用在该点为空,这就是
NullPointerException

的原因,非常感谢!!一直在挣扎,Android新手,所以非常感谢,你是真正的MVP
b1=(Button) b1.findViewById(R.id.button);
b1=(Button) findViewById(R.id.button);