[英]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. 因此,在一个Activity中,我在一个类中设置了一个ArrayList,而在另一个Activity中,我想获取ArrayList,但是当我尝试获取这些信息时,应用程序崩溃了。
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 致命异常:主进程:com.example.sander.onzeoptocht,PID:3749 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sander.onzeoptocht / com.example.sander .... t.deelnemer} :java.lang.ClassCastException:android.support.multidex.MultiDexApplication无法转换为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? 是不是getApplicationContext()
返回整个应用程序的上下文,而不仅仅是您的活动? Try using GetContext()
. 尝试使用GetContext()
。 Furthermore, the type of your ArrayList is alleDeelnemers, which looks kind of strange to me. 此外,您的ArrayList的类型是alleDeelnemers,这对我来说有点奇怪。
groetjes, 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. 如果要将值传递给另一个活动,则可以通过其他线程(例如, AsyncTask
将其链接,也可以将它们作为Extras传递。
Secondly I don't quite understand how you want to get your delmeneerInfo by setting it getApplicationContext()
. 其次,我不太了解如何通过设置getApplicationContext()
来获取delmeneerInfo。 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
您的错误消息中指出,不能将其android.support.multidex.MultiDexApplication cannot be cast to com.example.sander....mDataObject.deelnemerInfo
为您的类: 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/ 您也可以尝试使用自己的应用程序类扩展来创建类似全局变量的内容,请在此处查看: https : //www.mobomo.com/2011/05/how-to-use-application-object-of -android /
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.