简体   繁体   中英

Application crashed when I tried to get value from other activity, why?

So in one Activity I set an ArrayList in a class and in the other Activity I want to get the ArrayList, but when I try to get those information the application is crashing.

ERROR:

FATAL EXCEPTION: main Process: com.example.sander.onzeoptocht, PID: 3749 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sander.onzeoptocht/com.example.sander....t.deelnemer}: java.lang.ClassCastException: android.support.multidex.MultiDexApplication cannot be cast to com.example.sander....mDataObject.deelnemerInfo

Set Activity:

ArrayList<alleDeelnemers> deelnemers = new ArrayList<>();

//deelnemer is a class with ID and some strings.
deelnemers.add(deelnemer);

//set arraylist in class
deelnemerInfo info = new deelnemerInfo();
info.setDeelnemerGegevens(deelnemers);

Class:

public class deelnemerInfo extends Application {

ArrayList<alleDeelnemers> deelnemerGegevens;

public ArrayList<alleDeelnemers> getDeelnemerGegevens() {
    return deelnemerGegevens;
}
public void setDeelnemerGegevens(ArrayList<alleDeelnemers> deelnemerGegevens) {
    this.deelnemerGegevens = deelnemerGegevens;
}

Get Activity:

deelnemerInfo info = ((deelnemerInfo)getApplicationContext());

Log.e("test", String.valueOf(info.getDeelnemerGegevens().get(0).getAantalvolgnummer()));

Could it be that getApplicationContext() return the context of the entire application, not just your activity? Try using GetContext() . Furthermore, the type of your ArrayList is alleDeelnemers, which looks kind of strange to me.

groetjes,

First of all, you can't (at least I don't know of any way) just get a variable from another activity, when you define it in your set activity your scope is just that activity. When you want to pass a value into another activity you can either link them through a different thread, eg an AsyncTask or you pass them as Extras.

Secondly I don't quite understand how you want to get your delmeneerInfo by setting it getApplicationContext() . That does not return any variable you set in a previous activity, but rather just what it says, the application context. Which, your error message says it, cannot be cast to your class: android.support.multidex.MultiDexApplication cannot be cast to com.example.sander....mDataObject.deelnemerInfo

You can also try working with your own extension of the Application Class to create somehting like global variables, have a look here: https://www.mobomo.com/2011/05/how-to-use-application-object-of-android/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM