无法运行下载媒体活动

时间:2018-08-11 14:22:41

标签: java android

我对android编程还很陌生;我正在尝试排除故障并修复以下错误。该错误是由于FormMediaActivity错误导致的

08-11 16:40:00.480 5189-5189/org.odk.collect.android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.odk.collect.android, PID: 5189
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.odk.collect.android/org.odk.collect.android.activities.FormMediaUpdatedList}: java.lang.ClassCastException: org.odk.collect.android.activities.FormMediaUpdatedList cannot be cast to android.widget.AdapterView$OnItemClickListener
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassCastException: org.odk.collect.android.activities.FormMediaUpdatedList cannot be cast to android.widget.AdapterView$OnItemClickListener
        at org.odk.collect.android.activities.AppListActivity.setContentView(AppListActivity.java:127)
        at org.odk.collect.android.activities.FormMediaUpdatedList.setContentView(FormMediaUpdatedList.java:64)
        at org.odk.collect.android.activities.FormMediaUpdatedList.onCreate(FormMediaUpdatedList.java:115)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

此错误是我的android应用程序中的以下活动的结果:目的是下载新表单并显示过程对话框。我认为我的活动生命周期全错了,但是我不确定如何解决我的问题

public class FormMediaUpdatedList extends FormListActivity implements FormListDownloaderListener,
    DownloadFormsTaskListener, AuthDialogUtility.AuthDialogUtilityResultListener{
private static final String FORM_DOWNLOAD_LIST_SORTING_ORDER = "formDownloadListSortingOrder";

private static final int PROGRESS_DIALOG = 1;
private static final int AUTH_DIALOG = 2;
private static final int CANCELLATION_DIALOG = 3;


private static final String BUNDLE_SELECTED_COUNT = "selectedcount";
private static final String BUNDLE_FORM_MAP = "formmap";
private static final String DIALOG_TITLE = "dialogtitle";
private static final String DIALOG_MSG = "dialogmsg";
private static final String DIALOG_SHOWING = "dialogshowing";
private static final String FORMLIST = "formlist";
private static final String SELECTED_FORMS = "selectedForms";

public static final String FORMNAME = "formname";
private static final String FORMDETAIL_KEY = "formdetailkey";
public static final String FORMID_DISPLAY = "formiddisplay";

public static final String FORM_ID_KEY = "formid";
private static final String FORM_VERSION_KEY = "formversion";

private String alertMsg;
private boolean alertShowing;
private String alertTitle;

private AlertDialog alertDialog;
private ProgressDialog progressDialog;
private ProgressDialog cancelDialog;

private DownloadFormListTask downloadFormListTask;
private DownloadFormsTask downloadFormsTask;

private HashMap<String, FormDetails> formNamesAndURLs = new HashMap<String, FormDetails>();
private ArrayList<HashMap<String, String>> formList;
private final ArrayList<HashMap<String, String>> filteredFormList = new ArrayList<>();
private LinkedHashSet<String> selectedForms = new LinkedHashSet<>();

private static final boolean EXIT = true;
private static final boolean DO_NOT_EXIT = false;
private boolean shouldExit;
private static final String SHOULD_EXIT = "shouldexit";




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu);
    setTitle(getString(R.string.getupdated_forms));
    downloadUpdatedFiles();

}


/**
 * Starts the download task and shows the progress dialog.
 */
private void downloadFormList() {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

    if (ni == null || !ni.isConnected()) {
        ToastUtils.showShortToast(R.string.no_connection);
    } else {

        formNamesAndURLs = new HashMap<String, FormDetails>();
        if (progressDialog != null) {
            // This is needed because onPrepareDialog() is broken in 1.6.
            progressDialog.setMessage(getString(R.string.please_wait));
        }
        showDialog(PROGRESS_DIALOG);

        if (downloadFormListTask != null
                && downloadFormListTask.getStatus() != AsyncTask.Status.FINISHED) {
            return; // we are already doing the download!!!
        } else if (downloadFormListTask != null) {
            downloadFormListTask.setDownloaderListener(null);
            downloadFormListTask.cancel(true);
            downloadFormListTask = null;
        }

        downloadFormListTask = new DownloadFormListTask();
        downloadFormListTask.setDownloaderListener(this);
        downloadFormListTask.execute();

    }
}

@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    updateAdapter();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable(BUNDLE_FORM_MAP, formNamesAndURLs);
    outState.putString(DIALOG_TITLE, alertTitle);
    outState.putString(DIALOG_MSG, alertMsg);
    outState.putBoolean(DIALOG_SHOWING, alertShowing);
    outState.putBoolean(SHOULD_EXIT, shouldExit);
    outState.putSerializable(FORMLIST, formList);
    outState.putSerializable(SELECTED_FORMS, selectedForms);
}


@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case PROGRESS_DIALOG:
            Collect.getInstance().getActivityLogger().logAction(this,
                    "onCreateDialog.PROGRESS_DIALOG", "show");
            progressDialog = new ProgressDialog(this);
            DialogInterface.OnClickListener loadingButtonListener =
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Collect.getInstance().getActivityLogger().logAction(this,
                                    "onCreateDialog.PROGRESS_DIALOG", "OK");
                            // we use the same progress dialog for both
                            // so whatever isn't null is running
                            dialog.dismiss();
                            if (downloadFormListTask != null) {
                                downloadFormListTask.setDownloaderListener(null);
                                downloadFormListTask.cancel(true);
                                downloadFormListTask = null;
                            }
                            if (downloadFormsTask != null) {
                                showDialog(CANCELLATION_DIALOG);
                                downloadFormsTask.cancel(true);
                            }
                        }
                    };
            progressDialog.setTitle(getString(R.string.downloading_data));
            progressDialog.setMessage(alertMsg);
            progressDialog.setIcon(android.R.drawable.ic_dialog_info);
            progressDialog.setIndeterminate(true);
            progressDialog.setCancelable(false);
            progressDialog.setButton(getString(R.string.cancel), loadingButtonListener);
            return progressDialog;
        case AUTH_DIALOG:
            Collect.getInstance().getActivityLogger().logAction(this,
                    "onCreateDialog.AUTH_DIALOG", "show");

            alertShowing = false;

            return new AuthDialogUtility().createDialog(this, this, null);
        case CANCELLATION_DIALOG:
            cancelDialog = new ProgressDialog(this);
            cancelDialog.setTitle(getString(R.string.canceling));
            cancelDialog.setMessage(getString(R.string.please_wait));
            cancelDialog.setIcon(android.R.drawable.ic_dialog_info);
            cancelDialog.setIndeterminate(true);
            cancelDialog.setCancelable(false);
            return cancelDialog;
    }
    return null;
}




@Override
protected String getSortingOrderKey() {
    return FORM_DOWNLOAD_LIST_SORTING_ORDER;
}

@Override
protected void updateAdapter() {
    CharSequence charSequence = getFilterText();
    filteredFormList.clear();
    if (charSequence.length() > 0) {
        for (HashMap<String, String> form : formList) {
            if (form.get(FORMNAME).toLowerCase(Locale.US).contains(charSequence.toString().toLowerCase(Locale.US))) {
                filteredFormList.add(form);
            }
        }
    } else {
        filteredFormList.addAll(formList);
    }
    //sortList();
    if (listView.getAdapter() == null) {
        listView.setAdapter(new FormDownloadListAdapter(this, filteredFormList, formNamesAndURLs));
    } else {
        FormDownloadListAdapter formDownloadListAdapter = (FormDownloadListAdapter) listView.getAdapter();
        formDownloadListAdapter.setFromIdsToDetails(formNamesAndURLs);
        formDownloadListAdapter.notifyDataSetChanged();
    }

}


/**
 * starts the task to download the selected forms, also shows progress dialog
 */
private void downloadUpdatedFiles() {
    downloadFormsTask = new DownloadFormsTask();
    downloadFormsTask.setDownloaderListener(this);
}

@Override
public Object onRetainCustomNonConfigurationInstance() {
    if (downloadFormsTask != null) {
        return downloadFormsTask;
    } else {
        return downloadFormListTask;
    }
}

@Override
protected void onDestroy() {
    if (downloadFormListTask != null) {
        downloadFormListTask.setDownloaderListener(null);
    }
    if (downloadFormsTask != null) {
        downloadFormsTask.setDownloaderListener(null);
    }
    super.onDestroy();
}

@Override
protected void onResume() {
    if (downloadFormListTask != null) {
        downloadFormListTask.setDownloaderListener(this);
    }
    if (downloadFormsTask != null) {
        downloadFormsTask.setDownloaderListener(this);
    }
    if (alertShowing) {
        createAlertDialog(alertTitle, alertMsg, shouldExit);
    }
    super.onResume();
}

@Override
protected void onPause() {
    if (alertDialog != null && alertDialog.isShowing()) {
        alertDialog.dismiss();
    }
    super.onPause();
}

/*
 * Called when the form list has finished downloading. results will either contain a set of
 * <formname, formdetails> tuples, or one tuple of DL.ERROR.MSG and the associated message.
 */
public void formListDownloadingComplete(HashMap<String, FormDetails> result) {
    dismissDialog(PROGRESS_DIALOG);
    downloadFormListTask.setDownloaderListener(null);
    downloadFormListTask = null;

    if (result == null) {
        Timber.e("Formlist Downloading returned null.  That shouldn't happen");
        // Just displayes "error occured" to the user, but this should never happen.
        createAlertDialog(getString(R.string.load_remote_form_error),
                getString(R.string.error_occured), EXIT);
        return;
    }

    if (result.containsKey(DL_AUTH_REQUIRED)) {
        // need authorization
        showDialog(AUTH_DIALOG);
    } else if (result.containsKey(DL_ERROR_MSG)) {
        // Download failed
        String dialogMessage =
                getString(R.string.list_failed_with_error,
                        result.get(DL_ERROR_MSG).getErrorStr());
        String dialogTitle = getString(R.string.load_remote_form_error);
        createAlertDialog(dialogTitle, dialogMessage, DO_NOT_EXIT);
    } else {

        Toast.makeText(getBaseContext(), "Test out for an error" , Toast.LENGTH_SHORT ).show();
        // selectSupersededForms();

    }
}

/**
 * Creates an alert dialog with the given tite and message. If shouldExit is set to true, the
 * activity will exit when the user clicks "ok".
 */
private void createAlertDialog(String title, String message, final boolean shouldExit) {
    Collect.getInstance().getActivityLogger().logAction(this, "createAlertDialog", "show");
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(title);
    alertDialog.setMessage(message);
    DialogInterface.OnClickListener quitListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {
            switch (i) {
                case DialogInterface.BUTTON_POSITIVE: // ok
                    Collect.getInstance().getActivityLogger().logAction(this,
                            "createAlertDialog", "OK");
                    // just close the dialog
                    alertShowing = false;
                    // successful download, so quit
                    if (shouldExit) {
                        finish();
                    }
                    break;
            }
        }
    };
    alertDialog.setCancelable(false);
    alertDialog.setButton(getString(R.string.ok), quitListener);
    alertDialog.setIcon(android.R.drawable.ic_dialog_info);
    alertMsg = message;
    alertTitle = title;
    alertShowing = true;
    this.shouldExit = shouldExit;
    alertDialog.show();
}

@Override
public void progressUpdate(String currentFile, int progress, int total) {
    alertMsg = getString(R.string.fetching_file, currentFile, String.valueOf(progress), String.valueOf(total));
    progressDialog.setMessage(alertMsg);
}

@Override
public void formsDownloadingComplete(HashMap<FormDetails, String> result) {
    if (downloadFormsTask != null) {
        downloadFormsTask.setDownloaderListener(null);
    }

    if (progressDialog.isShowing()) {
        // should always be true here
        progressDialog.dismiss();
    }

    createAlertDialog(getString(R.string.download_forms_result), getDownloadResultMessage(result), EXIT);
}

public static String getDownloadResultMessage(HashMap<FormDetails, String> result) {
    Set<FormDetails> keys = result.keySet();
    StringBuilder b = new StringBuilder();
    for (FormDetails k : keys) {
        b.append(k.getFormName() + " ("
                + ((k.getFormVersion() != null)
                ? (Collect.getInstance().getString(R.string.version) + ": " + k.getFormVersion() + " ")
                : "") + "ID: " + k.getFormID() + ") - " + result.get(k));
        b.append("\n\n");
    }

    return b.toString().trim();
}

@Override
public void formsDownloadingCancelled() {
    if (downloadFormsTask != null) {
        downloadFormsTask.setDownloaderListener(null);
        downloadFormsTask = null;
    }
    if (cancelDialog.isShowing()) {
        cancelDialog.dismiss();
    }
}

@Override
public void updatedCredentials() {
    downloadFormList();
}

@Override
public void cancelledUpdatingCredentials() {
    finish();
}
}

有人可以帮我指出为什么我的活动引发错误吗?

0 个答案:

没有答案