0

I'm new to android development and Java programming. I am trying to create a separate class without a new user interface. Supposed I have a main activity wherein if I click a button it will initiate a class and call the method in it and display the text on the main activity.

Now I have create created a Main activity and a class which named as BluetoothOn: Please take a look at my code:

This is the main activity where the enablemyBluetooth method is the button click

public class MainActivity{
....

....
public void enablemyBluetooth(View view){             

    BluetoothOn ble = new BluetoothOn();
    ble.initializeBlue();
}

}

Now I create a class wherein I planned to do all the processes here without intervening on the Main activity of the program, like it is running on the background.. here in my code I just want to change the Textview but when I run it on my android it states that unfortunately the app stopped.

public class BluetoothOn {

private void initializeBlue(){

    textView1 =(TextView)findViewById(R.id.textView1);
    textView1.setText(BleisOn);
}
}

Please take note that I have compiled this program with no error/ all variables are declared. I have tried to create the Bluetooth class as private on the Main and it works. I just want to create it as another class on the package for organizing my code.

Is it really necessary that I have to create another activity if I want to create a separate class? Can anyone give me hints what should I do or declare? Please help thank you.

Update: thank you for all your answers, unfortunately I can't make this simple process to work on another class... I'm quite new to Java and still learning my way.. anyways here is what I encountered.

I followed the post suggested by unohu however it provides me an error on the Main that "The method to initializeBlue() in the type BluetoothOn is not applicable for the arguments.

Here is a more complete code of the classes:

Main activity:

public class MainActivity extends Activity { 

    public void enableBluetooth (View view){     

    BluetoothOn ble = new BluetoothOn();
    ble.initializeBlue();   // error on this part

    }       

}

BluetoothOn class:

 public class BluetoothOn{

        public TextView textView1;

    public void initializeBlue(View myView){
        String BleisOn = "connect me now";

        textView1 = (TextView)myView.findViewById(R.id.textView1);
        textView1.setText(BleisOn);
    }
}

note: I got a typo on the initial post, I declared the initialize as private but it should be public. I'm attempting to try luisdurazoa suggestion however I'm not familiar on Interface..

Nms03
  • 15
  • 5
  • Post your logcat output. – takendarkk Aug 31 '14 at 01:59
  • Not every class needs a layout. What you're looking for is helper class. http://programmers.stackexchange.com/questions/132067/difference-between-a-service-class-and-a-helper-class – Slay Aug 31 '14 at 02:17

3 Answers3

2

No, not at all, you dont need to have a separate activity for doing this, actually your approach is ok except that you should not be creating members of the interface outside the activity, like the textview, but the logic of the bluetooth is ok to have it outside, or database connections and such.

You are most likely to get an exception if you try to initialize a textView outside of an activity, instead use an interface between your class and your activity.

public interface IBluetooth{
    public void initBluetooth();
}

And then...

public class MainActivity implements IBluetooth{
....

....
public void enablemyBluetooth(View view){             

    BluetoothOn ble = new BluetoothOn(this);
}

}

@Override
initBluetooth(){
textView1 =(TextView)findViewById(R.id.textView1);
textView1.setText(BleisOn);
}

Your bluetooth class...

public class BluetoothOn {

BluetoothOn(IBluetooth callback){
    //this will do the ui operations you need to do in the activity
    callback.initBluetooth();
    //do your stuff here
}
}
Ringo
  • 850
  • 9
  • 24
0

The findViewById(id) method is called in an Activity and, as such, can only find views that belong to that Activity's layout.

This means that you can't call this method with an Id of a specific View in an Activity where that View wasn't declared.

In order to be able to get the View from your BluetoothOnclass you should pass the Activity to the class and then call the findViewById(id)method:

MainActivity

public class MainActivity{
....

....
public void enablemyBluetooth(View view){             

  BluetoothOn ble = new BluetoothOn(this);
  ble.initializeBlue();
 }

}

BluetoothOn

public class BluetoothOn {

    private void initializeBlue(Activity activity){

      textView1 =(TextView)activity.findViewById(R.id.textView1);
      textView1.setText(BleisOn);
     }
}
LuisF
  • 564
  • 1
  • 7
  • 18
0

No. Do not create new activity. Just pass the view object while calling initializeBlue(). Use (TextView)YourViewObject.findViewById(R.id.textView1) instead of(TextView)findViewById(R.id.textView1) in BluetoothOn.

public class MainActivity{

//Declare a textview 
private TextView textView;

 protected void onCreate(Bundle savedInstanceState) {
 .....
  textView =(TextView)myView.findViewById(R.id.textView1);
....
 }
....
public void enablemyBluetooth(View view){             

    BluetoothOn ble = new BluetoothOn();

   //Pass textview as argument
    ble.initializeBlue(textView);
    }
}

public class BluetoothOn {

private void initializeBlue(View myView){

textView1 =(TextView)myView.findViewById(R.id.textView1);
textView1.setText(BleisOn);
    }
}
Anirban Pal
  • 529
  • 4
  • 10
  • Hi I tried to follow your suggestion but I run to an error. public void enableBluetooth (View view){ BluetoothOn ble = new BluetoothOn(); ble.initializeBlue(); // I got an error here the method initializeBlue is not applicable for arguments } – Nms03 Aug 31 '14 at 07:11
  • Did you updated private void initializeBlue(View myView) instead of private void initializeBlue() in BluetoothOn.java? – Anirban Pal Aug 31 '14 at 07:24
  • Thanks for the fast reply. Yes, I tried, I updated my post..please check it...Don't know where to proceed. – Nms03 Aug 31 '14 at 07:31
  • I updated my answer..there was a typo...You have to call ble.initializeBlue(view); instead of ble.initializeBlue(); in MainActivity.java – Anirban Pal Aug 31 '14 at 07:35
  • Hello I tried it compiles however when I run it on the android and click the button I am faced with the same error "Unfortunately. This app has stopped.. – Nms03 Aug 31 '14 at 08:02
  • Updated my answer. Pass your textview object as argument as ble.initializeBlue(textView); I tested it. It should work. If it shows any error kindly share logcat details – Anirban Pal Aug 31 '14 at 08:20
  • Reply back if you face any kind of issue. – Anirban Pal Aug 31 '14 at 08:39
  • Hi unohu, thanks for taking time to read my posts, unfortunately I cant figure this out..I tried to follow your code and faced with variable errors, however I tried to manage and declare those variable and was able to compile the program. But when I run it the app doesn't start.. it prompts with an error that it has stopped. Ill upload the logcat, cant seem to figure out how can I share it on this site – Nms03 Aug 31 '14 at 09:00
  • I just uploaded it on my OneDrive, it includes the log cat, Main activity class and Bluetooth class. Please have a look: https://onedrive.live.com/redir?resid=8FE8F0BC302000CE%211038 Maybe I just declared the variables wrong...oh well guess I have to dig harder on this java thing... – Nms03 Aug 31 '14 at 09:08
  • Thanks a lot...I have updated working code for you. Kindly check https://onedrive.live.com/?cid=3A91BCBE966AEDF0&id=3A91BCBE966AEDF0%21105 – Anirban Pal Aug 31 '14 at 10:03
  • Wow! Thank you so much for helping me out, I've been figuring this simple process whole day.. I hope that I can get in touch with you, as I am on my way to create a project that will display output via Bluetooth device on the Android. I will study the code that you provided, can you please give me hints on what are the things that you add on the code like the onClicklistener, or any thing that I need to take note when I want to produce display from another class.. thank you so much for your help! more power! – Nms03 Aug 31 '14 at 10:26
  • 1. In onCreate() you should not add textView =(TextView)myView.findViewById(R.id.textView1); as myView is null here. So I removed that. 2. You never called enableBluetooth() so I created a button and call enableBluetooth() from onclick() of that button.A new method addListenerOnButton() created only to call enableBluetooth(). Hope this explanation helps you to understand updated code. – Anirban Pal Aug 31 '14 at 10:44
  • this approach may provoke memory leaks. – Ringo Sep 16 '14 at 21:30