-1

I have created other class for global variables now I just want to cast it like that:

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

    global = (class2)getApplicationContext();

I get error:

android.app.Application cannot be cast to eu.tools.test_training_home.class2

The class2:

public class class2 extends Application {

    private String name="name1";
    private int number =0;
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Testiest
  • 39
  • 1
  • 6

2 Answers2

0

Here getApplicationContext() returns an object of class Context but not class2.

Check https://stackoverflow.com/a/907368/1435132.

See Custom global Application class breaks with "android.app.Application cannot be cast to"

Community
  • 1
  • 1
Sangharsh
  • 2,999
  • 2
  • 15
  • 27
-1

The problem was the Consructor in Class2 into class2, i had 1 constuctor, 2 setter, 2 getter I have deleted the Constructor, then the error did not come

public class class2 extends Application {

private String name ="name1";
private int number =10;

public class2(String name, int number) { // this was the problem
    this.name = name;
    this.number = number;
}

public void setName(String name) {
    this.name = name;
}

public void setNumber(int number) {
    this.number = number;
}

public String getName() {
    return name;
}

public int getNumber() {
    return number;
}

}

Testiest
  • 39
  • 1
  • 6