This is a follow up scenario on my recent post wherein I tried to call a method from another class and respond with a change of TextView. Now I'm trying to add to initialize the Bluetooth Adapter on that method (BluetoothOn) however, I am faced again with the same error that the app stopped. On the other hand I can run the app with no issues if I declare the BLEadapter on the Main method.
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);
}
}
On My Bluetooth class
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);
}
}
}
Can anyone provide me knowledge on how can I call methods from my the main activity that will not cause the app to crash. I am just a beginner, kindly explain, what are the things that I need on a detailed manner, I just want to create other classes outside of the main and call the methods once a user interact on the Android app. I think that I just didn't pass values to the other class properly thats why it doesnt work on the android ...The app crashes once I tap the assigned button. what do I need on this code.. by the way here is the link on my prior post, creating a separate class without a user interface please help thank you good people...
Here is the log cat
09-01 13:26:27.307: I/ActivityManager(19336): Timeline: Activity_idle id: android.os.BinderProxy@42d54610 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