Android Call a Method from Another Class

Android Call a method from another class

You should use the following code :

Class2 cls2 = new Class2();
cls2.UpdateEmployee();

In case you don't want to create a new instance to call the method, you can decalre the method as static and then you can just call Class2.UpdateEmployee().

How to call a function from another class with its context (android, java)

Change your Adapter like this:

public class Adapter extends PagerAdapter {

private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;

private SetupActivity setupActivity;

public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
setupActivity = (SetupActivity) context;
}

@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
layoutInflater = LayoutInflater.from(context);
final View view = layoutInflater.inflate(R.layout.item, container, false);

TextView buttonDeleteCard = view.findViewById(R.id.textViewDeleteCard);

buttonDeleteCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setupActivity.deleteSession([...some title...]);
setupActivity.refreshAdapter();
}
});

container.addView(view, 0);
return view;
}
}

Why the error?

You pass the Context from SetupActivity to your Adapter, you save that reference in the field (context) but you mistakenly created a new instance of SetupActivity.

That's why you got the NullPointerException. The activity is your Context. It inherits from it. So just cast it in your constructor and use it later.

Calling a method from another class is causing app to crash

You can use another class's method by creating object of parent class.

See below example;

Here you want to use method from 'GameActivityPVP' class. So you need to create one object in this class only.

  public class GameActivityPVP extends MainActivity {

public static GameActivityPVP mGameActivity;

public GameActivityPVP getInstance(){
return mGameActivity; // assign value in onCreate() method.
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_layout_pvp);

mGameActivity = this; // Do not forget this, otherwise you'll get Exception here.
initializeButtons();
}

public void initializeButtons() {

button[0] = (Button) findViewById(R.id.button1);
}
}

Now use this Object in another class 'EasyMode' like this;

if(GameActivityPVP.getInstance()!=null){
GameActivityPVP.getInstance().initializeButtons();
}

How to call method of another class in android

In case of Android, class which extends Activity will maintain its life cycle methods. if method which is defined in different class other current running activity may be killed or in pause state. so it is suggested that if method which is reusable in application should in different class for example (AppManager singleton class) rather than being in single activity class

Java cannot find symbol when calling a method from another class

Method visAlleAtleter is not static so your can't call it as you do Klub.visAlleAtleter().
Just call method on object minKlub.visAlleAtleter().

Call MainActivity method from another class in android

Technically, the myMethod will get called, but since you create the MainActivity yourself, it's not attached to anything.

In general, you shouldn't create a new MainActivity instance this way. To open a new MainActivity, you use an Intent.

In your case, you should have a reference to the original MainActivity instance, and call this method there. Not create a new one in any way, as you already have it running.

An easy way to solve it:

MainActivity.this.myMethod("Hello there")

You don't have to store mContext . You already are inside of MainActivity.

So, the full code would be:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = findViewById(R.id.webview);
myWebView.loadUrl("http://www.google.com");
myWebView.addJavascriptInterface(new WebAppInterface(), "Android");
}

public void myMethod(String test){
Toast.makeText(this, test, Toast.LENGTH_SHORT).show();

}

public class WebAppInterface {

/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
MainActivity.this.myMethod("Hello there");
}
}

}

In fact, I think you can even avoid having the MainActivity.this. , and call myMethod directly.

Can't call method from another class in Java

You have to do something like below:

public class Persona {
...
//You should have instance of RegistroCompra
RegistroCompra registraCompra = new RegistroCompra();
public void setDestino() {
//Option 1: Explicitly call the method
registraCompra.seleccionarDestino();
destinoPasajero = registraCompra.obtenerDestino();
}
}

public class RegistroCompra {

private String destino;

public RegistroCompra(){
//Option 2 : Call the method in constructor
registraCompra();
}
public void seleccionarDestino() {
...
//Set the input to the class level variable destino
this.destino = input.nextLine();
}
public String obtenerDestino() {
return this.destino;
}

}



Related Topics



Leave a reply



Submit