Button.findViewById()上的NullPointerException

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

我的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 fie:

  

我的Logcat:

  06-13 21:32:20.500 11336-11336/hilz.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: hilz.myapplication, PID: 11336 java.lang.RuntimeException: Unable to start activity ComponentInfo{hilz.myapplication/hilz.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference at hilz.myapplication.MainActivity.onButoonListener(MainActivity.java:23) at hilz.myapplication.MainActivity.onCreate(MainActivity.java:20) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)            at android.app.ActivityThread.access$800(ActivityThread.java:151)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)            at android.os.Handler.dispatchMessage(Handler.java:102)            at android.os.Looper.loop(Looper.java:135)            at android.app.ActivityThread.main(ActivityThread.java:5257)            at java.lang.reflect.Method.invoke(Native Method)            at java.lang.reflect.Method.invoke(Method.java:372)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

您正在以错误的方式调用findViewById()方法。 它应该在Activity本身上调用,而不是在Button引用上调用。 更换

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

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

此时b1引用为null,并且是NullPointerException的原因。