2

在 android 4.0.3 上,如果您调用MenuItem.setActionView(null)然后旋转屏幕,我的应用程序将崩溃。

我想要做的是模拟actionbarGMail应用程序,如果您单击一个项目,它会变成progress bar.

但是,它现在保存了 的状态,ActionView当它尝试恢复它时,它会这样说。

无法启动活动 ComponentInfo{}:java.lang.ClassCastException:android.view.AbsSavedState$1 无法转换为 android.widget.ProgressBar$SavedState

有没有人有这方面的工作代码?或解决方法?

哦,actionbarsherlock如果那件事,我会使用。它不应该因为在4.03上它只是调用所有的系统调用。

代码:

OnCreate()
    refreshMenuActionView = new ProgressBar(this.getActivity());
    refreshMenuActionView.setIndeterminateDrawable(this.getActivity().getResources().getDrawable(R.drawable.refresh_spinner));

AsyncTask.onPostExecute
                if ( refreshMenuItem != null ) {
                    refreshMenuItem.setActionView(null);
                }

    @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.loadable_menu, menu);
    refreshMenuItem = menu.findItem(R.id.refreshMenuItem);
    if ( downloader != null && downloader.getStatus() == AsyncTask.Status.RUNNING ) {
            refreshMenuItem.setActionView(refreshMenuActionView);
    }
    searchMenuItem = menu.findItem(R.id.searchMenuItem);
    super.onCreateOptionsMenu(menu, inflater);
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/refreshMenuItem"
        android:title="@string/refresh_item_title"
        android:icon="@drawable/ic_menu_refresh"
        android:showAsAction="ifRoom" />
    <item
        android:id="@+id/searchMenuItem"
        android:title="@string/search_item_title"
        android:icon="@drawable/ic_menu_search"
        android:showAsAction="always" />
</menu>
4

1 回答 1

0

您能否在变成 ProgressBar 的单击周围发布更多代码?

我使用 ABS,下面是一些在用户单击按钮时启动 AsyncTask 和 ProgressBar 的代码(代码在 SherlockFragment 内):

public class LoginFragment extends SherlockFragment {
      ...

    loginButton = (Button) getActivity().findViewById(R.id.loginButton);
    loginButton.setOnClickListener(loginListener);


public OnClickListener loginListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
            ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Logging in...");
            LoginTask loginTask = new LoginTask((Polling) getActivity(), progressDialog);
            loginTask.execute();

然后,在整个 ASyncTask 中控制 ProgressDialog(每个方法中的 setVisibility):

public class LoginTask extends AsyncTask<String, Void, Integer> {

private ProgressDialog progressDialog;
private Polling activity;
private int id = -1;
private JSONParser jsonParser;
private static String loginURL = "";
private static String registerURL = "";
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
private int responseCode = 0;

public LoginTask(Polling activity, ProgressDialog progressDialog)
{
    this.activity = activity;
    this.progressDialog = progressDialog;
}

@Override
protected void onPreExecute()
{
    progressDialog.show();
}

protected Integer doInBackground(String... arg0) {

    // check for login response
    try {
        if (json.getString(KEY_SUCCESS) != null) {
            String res = json.getString(KEY_SUCCESS);

            if(Integer.parseInt(res) == 1){
                //user successfully logged in
                // Store user details in SQLite Database
                DatabaseHandler db = new DatabaseHandler(activity.getApplicationContext());
                JSONObject json_user = json.getJSONObject("user");
                //Log.v("name", json_user.getString(KEY_NAME));
                // Clear all previous data in database
                userFunction.logoutUser(activity.getApplicationContext());
                db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), 
                        json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                responseCode = 1;
                // Close Login Screen
                //finish();

            }else{
                responseCode = 0;
                // Error in login
            }
        }

    } catch (NullPointerException e) {
        e.printStackTrace();

    }
    catch (JSONException e) {
        e.printStackTrace();
    }

    return responseCode;
}

@Override
protected void onPostExecute(Integer responseCode)
{

    if (responseCode == 1) {
        progressDialog.dismiss();
        activity.loginReport(responseCode);


    }
    if (responseCode == 0) {
        progressDialog.dismiss();
        activity.loginReport(responseCode);

    }


}

}

于 2012-05-03T06:05:18.723 回答