gpt4 book ai didi

Android MenuItem setActionView(空)崩溃

转载 作者:行者123 更新时间:2023-11-29 13:59:11 26 4
gpt4 key购买 nike

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

我想做的是模拟 GMail 应用程序的 actionbar,如果您单击某个项目,它会变成一个进度条 .

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

Unable to start activity ComponentInfo{}: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to 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>

最佳答案

您能否发布更多有关变成 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(在每个方法中设置 Visibility):

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);

}


}

关于Android MenuItem setActionView(空)崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10408338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com