0

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
Community
  • 1
  • 1
Nms03
  • 15
  • 5
  • 1
    You say "the app stopped". Post the logcat output. – gosr Sep 01 '14 at 05:22
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Code-Apprentice Sep 01 '14 at 05:24
  • Guess: you pass a `TextView` to `initializeBlue()`. That method tries to *find* a `TextView` within a `View` - that's why `textView1` is probably `null`. – gosr Sep 01 '14 at 05:25
  • "how can I call methods from my the main activity that will not cause the app to crash" This is asking us to explain Object Oriented Programming and Design. There are many books written on this topic which is too large to write a short answer here. – Code-Apprentice Sep 01 '14 at 05:25
  • You followed the wrong answer on your last question. Look at luisdurazoa's answer. – Mike M. Sep 01 '14 at 05:27
  • Thanks guys for all the quick answers, I really appreciate it.. I apologize for asking quite the same questions.. I'll dig my way to use interface. Can you provide me any reference that it is quite friendly to noobs.. thank you. – Nms03 Sep 01 '14 at 05:40
  • I suggest the Oracle Java tutorials for learning basic Java and the Android developer documentation for learning the specifics of Android. – Code-Apprentice Sep 01 '14 at 05:47

1 Answers1

0

There are a few problems with your code:

  1. BluetoothOn should not have extends MainActivity.

  2. BluetoothOn should not know about any of the UI components. Passing a View to the initializeBlue() method is a poor design decision. See this answer to your previous question for a suggestion about how to separate your UI logic from your bluetooth logic.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Hi, why does the BluetoothOn should not have extends MainActivity...shouldn't it be declared so it can inherit methods and variables of the Main? – Nms03 Sep 01 '14 at 05:58
  • @Nms03 No. If you need methods from `Activity`, then you should pass an `Activity` reference to the constructor or methods of `BluetoothOn`. Trying to use these methods via inheritence is the main source of your problems because you are not respecting the Activity life-cycle. – Code-Apprentice Sep 01 '14 at 06:00
  • @Nms03 I believe you are trying to separate logic that cannot really be separated from your `MainActivity` because you require methods from it. I suggest that you first get this working within the confines of `MainActivity`. You can later look at what you can refactor into a separate class. – Code-Apprentice Sep 01 '14 at 06:06
  • Hi Code apprentice, you are right.. maybe I just need work it first on the Main Activity and try to separate it later. I think I'm mixing the java and android part on the wrong way...that means more research on my end. Thank you for taking time to read my post. – Nms03 Sep 01 '14 at 06:22