引起:java.lang.ClassCastException

时间:2012-07-26 05:54:40

标签: android android-intent

说明

当我点击标题栏上的按钮时,我收到了ClassCast异常。 我的源代码如下:

我的活动代码:

public void onClick(View v) {

    if (v.getId() == R.id.about_us) {

        if (isInternetConnected()) {
            System.out.println("about us");
            startActivity(new Intent(GridDisplayActivity.this, AboutUsActivity.class));
        } else {
            setAlertBox("No Internet Connection");
        }
    }
    if (v.getId() == R.id.hard_copy) {

        if (isInternetConnected()) {
            System.out.println("hard copy");
            startActivity(new Intent(GridDisplayActivity.this,HardCopyActivity.class));
        } else {
            setAlertBox("No Internet Connection");
        }
    }

}

自定义应用代码:

public class CustomApplication extends Application {

private Map<String, List<CustomAsyncTask<?,?,?>>> mActivityTaskMap;

public CustomApplication() {
    mActivityTaskMap = new HashMap<String, List<CustomAsyncTask<?,?,?>>>();
}

public void removeTask(CustomAsyncTask<?,?,?> task) {
    for (Entry<String, List<CustomAsyncTask<?,?,?>>> entry : mActivityTaskMap.entrySet()) {
        List<CustomAsyncTask<?,?,?>> tasks = entry.getValue();
        for (int i = 0; i < tasks.size(); i++) {
            if (tasks.get(i) == task) {
                tasks.remove(i);
                break;
            }
        }

        if (tasks.size() == 0) {
            mActivityTaskMap.remove(entry.getKey());
            return;
        }
    }
}

public void addTask(Activity activity, CustomAsyncTask<?,?,?> task) {
    String key = activity.getClass().getCanonicalName();
    List<CustomAsyncTask<?,?,?>> tasks = mActivityTaskMap.get(key);
    if (tasks == null) {
        tasks = new ArrayList<CustomAsyncTask<?,?,?>>();
        mActivityTaskMap.put(key, tasks);
    }

    tasks.add(task);
}

public void detach(Activity activity) {
    List<CustomAsyncTask<?,?,?>> tasks = mActivityTaskMap.get(activity.getClass().getCanonicalName());
    if (tasks != null) {
        for (CustomAsyncTask<?,?,?> task : tasks) {
            task.setActivity(null);
        }
    }
}

public void attach(Activity activity) {
    List<CustomAsyncTask<?,?,?>> tasks = mActivityTaskMap.get(activity.getClass().getCanonicalName());
    if (tasks != null) {
        for (CustomAsyncTask<?,?,?> task : tasks) {
            task.setActivity(activity);
        }
    }
  }

}

我得到的日志是:

07-26 11:06:10.567: E/AndroidRuntime(381):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-26 11:06:10.567: E/AndroidRuntime(381):  at dalvik.system.NativeStart.main(Native Method)
07-26 11:06:10.567: E/AndroidRuntime(381): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.bespoken.android.provider.CustomApplication
07-26 11:06:10.567: E/AndroidRuntime(381):  at com.bespoken.android.GridDisplayActivity.onSaveInstanceState(GridDisplayActivity.java:567)
07-26 11:06:10.567: E/AndroidRuntime(381):  at android.app.Activity.performSaveInstanceState(Activity.java:1114)
07-26 11:06:10.567: E/AndroidRuntime(381):  at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1184)
 07-26 11:06:10.567: E/AndroidRuntime(381):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2469)
 07-26 11:06:10.567: E/AndroidRuntime(381):     ... 12 more

请指导我。 提前谢谢。

1 个答案:

答案 0 :(得分:2)

请勿执行(CustomApplication)getApplication(),而是在static班级中设置CustomApplication mInstance字段,例如CustomApplication,将其设置在{CustomApplication.onCreate 1}}到this并在您想要访问CustomApplication'方法的任何地方使用它。

class CustomApplication extends Application
{

private static CustomApplication mInstance;

@Override
void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static CustomApplication getInstance() { return mInstance; }

public void mySpecificMethod() { .... }

}

然后你从你需要的地方打电话:

CustomApplication.getInstance()->mySpecificMethod()