2015-01-12 41 views
0

我試圖創建一個應用程序來激活和停用藍牙,找到配對的藍牙設備並掃描發現的藍牙設備。我將權限添加到AndroidManifest.xml。一切似乎都沒問題。但是,當我在設備上調試應用程序(三星Galaxy Tab 3 Lite)時,我得到NullPointerException和應用程序崩潰。你有什麼想法可能會導致這個問題?請在下面找到我的代碼和異常堆棧跟蹤。NullPointerException當我嘗試測試已實現的藍牙功能

​​

這裏是我的logcat的文件:

Caused by: java.lang.NullPointerException 
     at dyankov.mylibraryrecommender.MainActivities.GUI.AroundMeActivity.onCreate(AroundMeActivity.java:60) 
     at android.app.Activity.performCreate(Activity.java:5326) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) 

線60上的代碼是:

turnOn.setOnClickListener(new OnClickListener() { ... } 

+1

你在做什麼叫@'線的AroundMeActivity.java' 60?那是我看的地方。 – Willis

+0

@Willis我編輯該文件,以便您可以看到第60行的代碼。謝謝 – John

+2

以及很明顯,'turnOn'是'null' – chancea

回答

0

turnOn = (Button)findViewById(R.id.turnOn)和所有Button對象initializat離子之前turnOn.setEnabled(false);

+0

我剛剛在Eclipse Luna而不是Android Studio上測試了應用程序,並且出人意料地發現一切正常。你知道爲什麼會這樣嗎? – John

0

您嘗試在爲其設置值之前使用按鈕。

試試這個代碼:

text = (TextView) findViewById(R.id.text); 
turnOn = (Button)findViewById(R.id.turnOn); 
if(myBluetoothAdapter == null) { 
turnOn.setEnabled(false); 
offBtn.setEnabled(false); 
listBtn.setEnabled(false); 
findBtn.setEnabled(false); 
text.setText("Status: not supported"); 
Toast.makeText(getApplicationContext(),"Your device does not support Bluetooth", 
Toast.LENGTH_LONG).show(); 
} else { 

turnOn.setOnClickListener(new OnClickListener() { 
相關問題