2017-05-18 36 views
1

我正在尝试制作其屏幕始终处于横向模式的应用程序。在button.setonclick监听器上抛出空指针异常

我用setRequestedOrientation()函数来实现。我用一些按钮来创建一个计算器。

但是,当我试图检查它的一些功能时,通过在模拟器上运行它,我得到了错误Unfortunately, Your app has been stopped

基本上,它是按nullpointer exception在按钮的setOnClickListener功能。我不知道为什么如此。 logcat错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.john.mycalci/com.example.john.mycalci.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 
    at com.example.john.mycalci.MainActivity.onCreate(MainActivity.java:79) 
    at android.app.Activity.performCreate(Activity.java:6662) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  
    at android.app.ActivityThread.-wrap12(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:154)  
    at android.app.ActivityThread.main(ActivityThread.java:6077)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

这是我的java代码。我想使用layout-land资源。

+3

的可能的复制[什么是空指针异常,怎么解决呢?(http://stackoverflow.com/questions/218384/what-is-a -nullpointerexception-and-how-do-i-fix-it) – Turing85

+0

确保所有的ID都可用于这两种布局 – FlanschiFox

+0

哪个按钮使应用程序崩溃? –

回答

1

在将onClickListener设置为Button之前,为ORIENTATION_LANDSCAPE添加检查。

试试这个:

................. 
........................ 

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
{ 
    bRad.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String s = ((Button) v).getText().toString(); 
      if(s=="Rad") { 
       bDegree.setText("Degree"); 
       bRad.setText(""); 
      } 

     } 
    }); 
    bDegree.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String s = ((Button) v).getText().toString(); 
      if(s=="Rad") { 
       bDegree.setText(""); 
       bRad.setText("Rad"); 
      } 

     } 
    }); 


    bInv.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      clicks += 1; 
      if (clicks % 2 == 1) { 
       bCos.setText(Html.fromHtml("cos<sup>-1</sup>")); 
       bSin.setText(Html.fromHtml("sin<sup>-1</sup>")); 
       bTan.setText(Html.fromHtml("tan<sup>-1</sup>")); 
       bLn.setText(Html.fromHtml("e<sup>x</sup>")); 
       bLog.setText(Html.fromHtml("10<sup>x</sup>")); 
       bSqrt.setText(Html.fromHtml("x<sup>2</sup>")); 
       bExpo.setText("root"); 
      } 
      else{ 
       bCos.setText("cos"); 
       bSin.setText("sin"); 
       bTan.setText("tan"); 
       bLn.setText("ln"); 
       bLog.setText("log"); 
       bSqrt.setText("sqrt"); 
       bExpo.setText("^"); 

      } 
     } 
    }); 

}