creating a separate class without a user interface

2019-09-07 04:02发布

问题:

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..

回答1:

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);
    }
}


回答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
}
}


回答3:

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);
     }
}