我想访问一个活动的arraylist到另一个(没有使用意图)但得到classcastException

时间:2014-08-02 05:59:39

标签: android arraylist classcastexception

我想从一个活动到另一个活动访问一个arrayList,我试过如下,但它给了我ClassCast异常, 的 logcat的

08-02 11:33:37.438: E/AndroidRuntime(1940): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eps.blancatours/com.eps.blancatours.ui.AddBookingpopupActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.eps.blancatours.ui.DestinationsActivity
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.ActivityThread.access$700(ActivityThread.java:168)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.os.Looper.loop(Looper.java:176)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.ActivityThread.main(ActivityThread.java:5493)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at java.lang.reflect.Method.invoke(Method.java:525)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at dalvik.system.NativeStart.main(Native Method)
08-02 11:33:37.438: E/AndroidRuntime(1940): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.eps.blancatours.ui.DestinationsActivity
08-02 11:33:37.438: E/AndroidRuntime(1940):     at com.eps.blancatours.ui.AddBookingpopupActivity.onCreate(AddBookingpopupActivity.java:30)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.Activity.performCreate(Activity.java:5372)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
08-02 11:33:37.438: E/AndroidRuntime(1940):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)

destinationActivity.java

 public class MyApp extends Application {
        public ArrayList<String> getState() {
            return names;
        }

        public void setState(ArrayList<String> s) {
            names = s;
        }
    }

secondactivity.java

   MyApp appState = ((MyApp)getApplicationContext());
          dest = appState.getState();

4 个答案:

答案 0 :(得分:1)

错误在Logcat中明确提到

<强> Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.eps.blancatours.ui.DestinationsActivity

这意味着您将getApplicationContext()投射到不可能的活动中。

并且您希望对应用程序中的所有活动使用相同的ArrayList,您需要在Application class

中声明ArrayList

Application class而不是destinationActivity.java中声明此代码。

public ArrayList<String> names;

names = new ArrayList<String>();

public ArrayList<String> getState() {
    return names;
}

public void setState(ArrayList<String> s) {
    names = s;
}

要在任何其他Activity中使用此ArrayList,您需要使用以下代码。

MyApplicationClass applicaionClass = (MyApplicationClass) getApplication();

ArrayList<String> mStateNames = (ArrayList<String>) applicaionClass.getState();

答案 1 :(得分:0)

因为getApplicationContext()将返回Application对象,而不是Activity。

此外,dest = new ArrayList<String>()如果您立即要覆盖它,则没有任何意义。

答案 2 :(得分:0)

您无法将getApplicationContext投射到第一个活动,它只会返回应用程序的引用&#39;上下文不是您的destinationActivity,因此会为您提供ClassCast exception

相反,您可以在启动secondactivity

的情况下通过意图传递arraylist

答案 3 :(得分:0)

我已将我的arrayList声明为我的Constant类中的全局变量,并将其声明为staic,我的代码如下所示;

public static ArrayList<HashMap<String, String>> dest = new ArrayList<HashMap<String, String>>();

使用

dest = new ArrayList<HashMap<String, String>>();
        dest = Const.dest;