0

描述:

当我单击标题栏上的按钮时,我得到了 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

请指导我。提前致谢。

4

1 回答 1

2

不要这样做(CustomApplication)getApplication(),而是在类中有一个static CustomApplication字段,例如,将其设置在to中,并在要访问s 方法的任何地方使用它。mInstanceCustomApplicationCustomApplication.onCreatethisCustomApplication'

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()
于 2012-07-26T06:11:58.413 回答