我有这个片段,但是每当我尝试更改旋转时它就会崩溃并显示错误消息。我有一个带有导航抽屉的Activity,里面有framelayout用于显示片段,除了这个片段之外所有其他片段都有效......
以下是片段的代码:
public class EventFragment extends Fragment implements OnNavigationListener,
View.OnClickListener, View.OnKeyListener {
private int mEventId;
private Bundle bundle;
private String mLatitude, mLongitude, mTitle, mContentMessage, mDateTime,
mCurentLatitude, mCurentLongitude, mShowtimeId;
private boolean noNavigationForYou;
private RatingBar mRatingBarNotSet, mRatingBarSet;
private ScrollView mScrollView;
private ProgressBar mProgressLoader;
private TextView mTitleView, mContentMessageView, mDateTimeView,
mUserRatingSetText;
private LinearLayout mRatingSet, mRatingNotSet;
private View rootView, content, loader;
private Button mEditRatingButton, mViewComentsButton,
mAllRatingsNotSetButton, mAllRatingsSetButton, mShowOnMapButton,
mShowNavigationButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_event, container, false);
content = inflater.inflate(R.layout.fragment_event_content, container,
false);
loader = inflater.inflate(R.layout.fragment_event_loader, container,
false);
noNavigationForYou = false;
setUpParentStuff();
setUpTitle();
mViewComentsButton = (Button) content
.findViewById(R.id.event_details_coments_button);
mAllRatingsNotSetButton = (Button) content
.findViewById(R.id.event_details_ratings_button);
mAllRatingsSetButton = (Button) content
.findViewById(R.id.event_details_ratings_set_button);
mEditRatingButton = (Button) content
.findViewById(R.id.event_details_edit_user_rating_button);
mShowOnMapButton = (Button) content
.findViewById(R.id.event_navigation_showonmap);
mShowNavigationButton = (Button) content
.findViewById(R.id.event_navigation_shownavigation);
if (noNavigationForYou) {
mShowNavigationButton.setVisibility(View.GONE);
}
Button test = (Button) content.findViewById(R.id.tst_button);
test.setOnClickListener(this);
mViewComentsButton.setOnClickListener(this);
mAllRatingsSetButton.setOnClickListener(this);
mAllRatingsNotSetButton.setOnClickListener(this);
mEditRatingButton.setOnClickListener(this);
mShowOnMapButton.setOnClickListener(this);
mShowNavigationButton.setOnClickListener(this);
mProgressLoader = (ProgressBar) loader
.findViewById(R.id.event_loader_bar);
mProgressLoader.setIndeterminate(true);
mRatingBarNotSet = (RatingBar) content
.findViewById(R.id.event_details_rating_bar_not_set);
mRatingBarSet = (RatingBar) content
.findViewById(R.id.event_details_rating_bar);
mRatingBarNotSet.setOnRatingBarChangeListener(new RatingChangeListener(
mEventId, getActivity(), getFragmentManager()));
mTitleView = (TextView) content.findViewById(R.id.event_details_title);
mContentMessageView = (TextView) content
.findViewById(R.id.event_details_subtitle);
mDateTimeView = (TextView) content
.findViewById(R.id.event_details_date);
mUserRatingSetText = (TextView) content
.findViewById(R.id.event_details_user_rating_textview);
mScrollView = (ScrollView) rootView.findViewById(R.id.scrollView1);
mScrollView.addView(loader);
mRatingSet = (LinearLayout) content
.findViewById(R.id.event_details_rating_set_view);
mRatingNotSet = (LinearLayout) content
.findViewById(R.id.event_details_rating_not_set_view);
setHasOptionsMenu(true); // aby bolo share option
rootView.setFocusableInTouchMode(true);
rootView.requestFocus();
rootView.setOnKeyListener(this);
new FetchEventDetails().execute(URL);
return rootView;
}
private void setUpTitle() {
getActivity().getActionBar().setTitle(bundle.getString("title"));
}
private void setUpParentStuff() {
bundle = getArguments();
mEventId = bundle.getInt("id");
mCurentLatitude = bundle.getString("userCurLat");
mCurentLongitude = bundle.getString("userCurLng");
mShowtimeId = bundle.getString("showTimeId");
if (mCurentLatitude == null) {
noNavigationForYou = true;
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.action_bar_share_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
MenuItem item = menu.findItem(R.id.menu_item_share);
ShareActionProvider myShareActionProvider = (ShareActionProvider) item
.getActionProvider();
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, mTitle + " - " + mDateTime);
myIntent.setType("text/plain");
myShareActionProvider.setShareIntent(myIntent);
}
@Override
public void onResume() {
super.onResume();
getActivity().getActionBar().setTitle(bundle.getString("title"));
}
@Override
public void onPause() {
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
super.onPause();
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
return false;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.event_details_coments_button:
Intent i = new Intent(getActivity(), ComentsActivity.class);
i.putExtra("id", mEventId);
getActivity().startActivity(i);
break;
case R.id.event_details_ratings_button:
Intent in = new Intent(getActivity(), RatingsActivity.class);
in.putExtra("id", mEventId);
getActivity().startActivity(in);
break;
case R.id.event_details_ratings_set_button:
Intent inn = new Intent(getActivity(), RatingsActivity.class);
inn.putExtra("id", mEventId);
getActivity().startActivity(inn);
break;
case R.id.event_details_edit_user_rating_button:
DialogHelper dh = new DialogHelper(getActivity(),
getFragmentManager());
AlertDialog dialog = dh.showNonEmptyRatingDialog(mRatingBarSet,
mEventId, mUserRatingSetText.getText().toString());
break;
case R.id.event_navigation_showonmap:
Intent newMap = new Intent(getActivity(), MapActivity.class);
newMap.putExtra("lat", mLatitude);
newMap.putExtra("lng", mLongitude);
newMap.putExtra("etitle", mTitle);
getActivity().startActivity(newMap);
break;
case R.id.event_navigation_shownavigation:
Intent newNavigation = new Intent(getActivity(), MapActivity.class);
newNavigation.putExtra("lat", mLatitude);
newNavigation.putExtra("lng", mLongitude);
newNavigation.putExtra("etitle", mTitle);
newNavigation.putExtra("clat", mCurentLatitude);
newNavigation.putExtra("clng", mCurentLongitude);
getActivity().startActivity(newNavigation);
break;
default:
break;
}
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
getFragmentManager().popBackStack("events", // null
getFragmentManager().POP_BACK_STACK_INCLUSIVE);
getActivity().getActionBar().setTitle(bundle.getString("title_r"));
return true;
} else {
return false;
}
}
@Override
public void onDestroy() {
FragmentManager fm = getActivity().getFragmentManager();
for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
fm.popBackStack();
}
super.onDestroy();
}
private class FetchEventDetails extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
JSONParser jsonParser = new JSONParser();
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("eventid", String
.valueOf(mEventId)));
parameters.add(new BasicNameValuePair("showid", mShowtimeId));
JSONObject jobj = jsonParser.getJSONFromUrl(URL, parameters, "GET");
Log.i("jobj", jobj.toString());
try {
JSONArray pom = jobj.getJSONArray(KEY_RESULTS);
for (int i = 0; i < pom.length(); i++) {
JSONObject eventDetailsJ = pom.getJSONObject(i);
mTitle = eventDetailsJ.getString(KEY_TITLE);
mDateTime = eventDetailsJ.getString(KEY_DATE);
mContentMessage = eventDetailsJ.getString(KEY_DETAILS);
mLatitude = eventDetailsJ.getString(KEY_LAT);
mLongitude = eventDetailsJ.getString(KEY_LNG);
mDateTime = mDateTime.substring(8, 10) + "."
+ mDateTime.substring(5, 7) + "."
+ mDateTime.substring(0, 4) + " "
+ mDateTime.substring(11, 16) + ", ";
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
mTitleView.setText(mTitle);
mContentMessageView.setText(mContentMessage);
mDateTimeView.setText(mDateTime);
new FetchUserRating().execute(URL);
super.onPostExecute(result);
}
}
private class FetchUserRating extends
AsyncTask<String, String, List<String>> {
private String result = null;
private String result_text = null;
private UserFunctions userFunctions = new UserFunctions();
private List<String> results;
@Override
protected List<String> doInBackground(String... params) {
results = new ArrayList<String>();
JSONParser jsonParser = new JSONParser();
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", userFunctions
.getUserName(getActivity())));
parameters.add(new BasicNameValuePair("id_event", String
.valueOf(mEventId)));
JSONObject jobj = jsonParser.getJSONFromUrl(URL, parameters, "GET");
try {
JSONArray jarray = jobj.getJSONArray(KEY_RESULTS);
for (int i = 0; i < jarray.length(); i++) {
JSONObject ratingJ = jarray.getJSONObject(i);
if (ratingJ.has(KEY_USER_RATING_VAL)) {
results.add(ratingJ.getString(KEY_USER_RATING_VAL));
results.add(ratingJ.getString(KEY_USER_RATING_TEXT));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return results;
}
@Override
protected void onPostExecute(List<String> results) {
if (results.size() > 0) {
result = results.get(0);
result_text = results.get(1);
mRatingBarSet.setOnRatingBarChangeListener(null);
mRatingBarSet.setRating(Float.parseFloat(result));
mRatingBarSet.setIsIndicator(true);
mUserRatingSetText.setText(result_text);
mRatingNotSet.setVisibility(View.GONE);
} else {
mRatingSet.setVisibility(View.GONE);
}
mScrollView.removeAllViews();
mScrollView.addView(content);
super.onPostExecute(results);
}
}
}
错误:
04-08 20:49:16.465: E/AndroidRuntime(17257): FATAL EXCEPTION: main
04-08 20:49:16.465: E/AndroidRuntime(17257): Process: com.kulturnepodujatia, PID: 17257
04-08 20:49:16.465: E/AndroidRuntime(17257): java.lang.RuntimeException: Unable to destroy activity {com.kulturnepodujatia/com.kulturnepodujatia.DashboardActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3517)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3535)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3735)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.ActivityThread.access$900(ActivityThread.java:145)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.os.Looper.loop(Looper.java:136)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.ActivityThread.main(ActivityThread.java:5081)
04-08 20:49:16.465: E/AndroidRuntime(17257): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 20:49:16.465: E/AndroidRuntime(17257): at java.lang.reflect.Method.invoke(Method.java:515)
04-08 20:49:16.465: E/AndroidRuntime(17257): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-08 20:49:16.465: E/AndroidRuntime(17257): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-08 20:49:16.465: E/AndroidRuntime(17257): at dalvik.system.NativeStart.main(Native Method)
04-08 20:49:16.465: E/AndroidRuntime(17257): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.FragmentManagerImpl.popBackStack(FragmentManager.java:484)
04-08 20:49:16.465: E/AndroidRuntime(17257): at com.kulturnepodujatia.fragments.EventFragment.onDestroy(EventFragment.java:262)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.Fragment.performDestroy(Fragment.java:1913)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1881)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.Activity.performDestroy(Activity.java:5402)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
04-08 20:49:16.465: E/AndroidRuntime(17257): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3504)
答案 0 :(得分:0)
使用commitAllowingStateLoss(),它应该没问题。