2017-05-18 35 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("^"); 

      } 
     } 
    }); 

}