這是我最近發佈的後續場景,其中我試圖調用另一個類的方法並響應TextView的更改。現在我試圖添加初始化該方法的藍牙適配器(BluetoothOn),但是,我又遇到了與應用程序停止相同的錯誤。另一方面,如果我在Main方法中聲明BLEadapter,則可以運行該應用程序而不會出現問題。爲什麼我不能初始化另一個類的方法藍牙
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
private TextView textView;
//private View myView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView =(TextView)findViewById(R.id.textView1);
addListenerOnButton();
}
private void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
enableBluetooth(arg0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void enableBluetooth (View view){
BluetoothOn ble = new BluetoothOn();
ble.initializeBlue(textView);
}
}
在我的藍牙類
public class BluetoothOn extends MainActivity {
private TextView textView1;
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
public void initializeBlue(View myView){
String BleisOn = "Bluetooth enabled !!!!";
textView1 = (TextView)myView.findViewById(R.id.textView1);
textView1.setText(BleisOn);
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
誰能給我知識上我怎麼能叫從我的主要活動方式,不會導致應用程序崩潰。我只是一個初學者,請詳細解釋我需要什麼東西,我只想在主體外創建其他類,並在用戶與Android應用程序交互後調用方法。我認爲我只是沒有正確地將值傳遞給其他類,這就是爲什麼它不能在android上工作...一旦我點擊分配的按鈕,應用程序崩潰。我需要什麼這個代碼..這裏的路是我以前的帖子鏈接,creating a separate class without a user interface請幫忙謝謝好心人......
這裏是日誌貓
09-01 13:26:27.307: I/ActivityManager(19336): Timeline: Activity_idle id: [email protected] time:341763403
09-01 13:26:39.627: D/AndroidRuntime(21289): Shutting down VM
09-01 13:26:39.627: W/dalvikvm(21289): threadid=1: thread exiting with uncaught exception (group=0x4156cd88)
09-01 13:26:39.627: E/AndroidRuntime(21289): FATAL EXCEPTION: main
09-01 13:26:39.627: E/AndroidRuntime(21289): Process: com.example.thisapp, PID: 21289
09-01 13:26:39.627: E/AndroidRuntime(21289): java.lang.IllegalStateException: System services not available to Activities before onCreate()
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.app.Activity.getSystemService(Activity.java:4532)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.example.thisapp.BluetoothOn.initializeBlue(BluetoothOn.java:27)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.example.thisapp.MainActivity.enableBluetooth(MainActivity.java:71)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.example.thisapp.MainActivity$1.onClick(MainActivity.java:43)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.view.View.performClick(View.java:4569)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.view.View$PerformClick.run(View.java:18553)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.os.Handler.handleCallback(Handler.java:733)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.os.Handler.dispatchMessage(Handler.java:95)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.os.Looper.loop(Looper.java:212)
09-01 13:26:39.627: E/AndroidRuntime(21289): at android.app.ActivityThread.main(ActivityThread.java:5151)
09-01 13:26:39.627: E/AndroidRuntime(21289): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 13:26:39.627: E/AndroidRuntime(21289): at java.lang.reflect.Method.invoke(Method.java:515)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
09-01 13:26:39.627: E/AndroidRuntime(21289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
09-01 13:26:39.627: E/AndroidRuntime(21289): at dalvik.system.NativeStart.main(Native Method)
09-01 13:26:41.487: I/Process(21289): Sending signal. PID: 21289 SIG: 9
你說「應用程序已停止」。發佈logcat輸出。 – eightx2 2014-09-01 05:22:36
[可惜MyApp已停止。我怎麼能解決這個問題?](http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this) – 2014-09-01 05:24:41
猜測:你傳遞一個'TextView'到'initializeBlue ()'。該方法試圖*在'View'中查找* TextView - 這就是爲什麼'textView1'可能是'null'。 – eightx2 2014-09-01 05:25:19