旋转屏幕或最小化应用程序时片段崩溃
Fragment crashing when rotate screen or minimise theapplication
我正在从第一个片段转到第二个片段。当我在第二个片段上旋转或最小化屏幕时,应用程序崩溃。早些时候当我用来最小化应用程序时,第一个片段被调用(不知道为什么),现在它在这里崩溃是代码:
2nd Fragment :
public class SubPlaceFragment extends Fragment {
public static final String LOG_TAG = SubPlaceFragment.class.getSimpleName();
private OnFragmentInteractionListener mListener;
public SubPlaceFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sub_place, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onPause() {
super.onPause();
Log.i(LOG_TAG,"onPause");
}
@Override
public void onResume() {
super.onResume();
Log.i(LOG_TAG, "onResume");
}
@Override
public void onStop() {
super.onStop();
Log.i(LOG_TAG, "onStop");
}
@Override
public void onStart() {
super.onStart();
Log.i(LOG_TAG, "onStart");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(LOG_TAG, "onActivityCreated");
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
这是我的 logcat :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.manish.planetdelhi, PID: 16516
java.lang.RuntimeException: Unable to resume activity {com.example.manish.planetdelhi/com.example.manish.planetdelhi.MainActivity}: java.lang.ClassCastException: com.example.manish.planetdelhi.SubPlaceFragment cannot be cast to com.example.manish.planetdelhi.PlaceListFragment
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2812)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.example.manish.planetdelhi.SubPlaceFragment cannot be cast to com.example.manish.planetdelhi.PlaceListFragment
at com.example.manish.planetdelhi.MainActivity.AddPlaceFragment(MainActivity.java:74)
at com.example.manish.planetdelhi.MainActivity.onStart(MainActivity.java:80)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
at android.app.Activity.performStart(Activity.java:5258)
at android.app.Activity.performRestart(Activity.java:5314)
at android.app.Activity.performResume(Activity.java:5319)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2797)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
这是我的完整主要 activity 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
AddPlaceFragment(savedInstanceState);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void AddPlaceFragment(Bundle loadingFirstTime)
{
// calling placeListFragment for the first time
if (loadingFirstTime == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, placeListFragment).addToBackStack(placeListFragment.getTag());
fragmentTransaction.commit();
}
}
// calling placeListFragment when screen is rotated
private void AddPlaceFragment()
{
placeListFragment = (PlaceListFragment) getFragmentManager().findFragmentById(R.id.fragment_container);
}
@Override
protected void onStart() {
super.onStart();
AddPlaceFragment();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
int fragmentCount = getFragmentManager().getBackStackEntryCount();
if(fragmentCount > 1)
{
getFragmentManager().popBackStack();
}
else {
finish();
}
}
@Override
public void DelhiPlaceClicked(int paramInt, String paramString) // meathod invoked from placelistfragment
{
Bundle localBundle = new Bundle();
localBundle.putInt("_id", paramInt);
localBundle.putString("sub_place_name", paramString);
SubPlaceFragment subfragment = new SubPlaceFragment();
subfragment.setArguments(localBundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, subfragment).addToBackStack(subfragment.getTag());
fragmentTransaction.commit();
}
感谢您的帮助:)
您正在通过以下方式添加您的 subfragment
:
fragmentTransaction.replace(R.id.fragment_container, subfragment)
现在,请致电:
getFragmentManager().findFragmentById(R.id.fragment_container);
只会 return 你的 subfragment
。因此,您将获得类广播异常
我正在从第一个片段转到第二个片段。当我在第二个片段上旋转或最小化屏幕时,应用程序崩溃。早些时候当我用来最小化应用程序时,第一个片段被调用(不知道为什么),现在它在这里崩溃是代码:
2nd Fragment :
public class SubPlaceFragment extends Fragment {
public static final String LOG_TAG = SubPlaceFragment.class.getSimpleName();
private OnFragmentInteractionListener mListener;
public SubPlaceFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sub_place, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onPause() {
super.onPause();
Log.i(LOG_TAG,"onPause");
}
@Override
public void onResume() {
super.onResume();
Log.i(LOG_TAG, "onResume");
}
@Override
public void onStop() {
super.onStop();
Log.i(LOG_TAG, "onStop");
}
@Override
public void onStart() {
super.onStart();
Log.i(LOG_TAG, "onStart");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(LOG_TAG, "onActivityCreated");
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
这是我的 logcat :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.manish.planetdelhi, PID: 16516
java.lang.RuntimeException: Unable to resume activity {com.example.manish.planetdelhi/com.example.manish.planetdelhi.MainActivity}: java.lang.ClassCastException: com.example.manish.planetdelhi.SubPlaceFragment cannot be cast to com.example.manish.planetdelhi.PlaceListFragment
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2812)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: com.example.manish.planetdelhi.SubPlaceFragment cannot be cast to com.example.manish.planetdelhi.PlaceListFragment
at com.example.manish.planetdelhi.MainActivity.AddPlaceFragment(MainActivity.java:74)
at com.example.manish.planetdelhi.MainActivity.onStart(MainActivity.java:80)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1194)
at android.app.Activity.performStart(Activity.java:5258)
at android.app.Activity.performRestart(Activity.java:5314)
at android.app.Activity.performResume(Activity.java:5319)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2797)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2845)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1252)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
这是我的完整主要 activity 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
AddPlaceFragment(savedInstanceState);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private void AddPlaceFragment(Bundle loadingFirstTime)
{
// calling placeListFragment for the first time
if (loadingFirstTime == null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, placeListFragment).addToBackStack(placeListFragment.getTag());
fragmentTransaction.commit();
}
}
// calling placeListFragment when screen is rotated
private void AddPlaceFragment()
{
placeListFragment = (PlaceListFragment) getFragmentManager().findFragmentById(R.id.fragment_container);
}
@Override
protected void onStart() {
super.onStart();
AddPlaceFragment();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
int fragmentCount = getFragmentManager().getBackStackEntryCount();
if(fragmentCount > 1)
{
getFragmentManager().popBackStack();
}
else {
finish();
}
}
@Override
public void DelhiPlaceClicked(int paramInt, String paramString) // meathod invoked from placelistfragment
{
Bundle localBundle = new Bundle();
localBundle.putInt("_id", paramInt);
localBundle.putString("sub_place_name", paramString);
SubPlaceFragment subfragment = new SubPlaceFragment();
subfragment.setArguments(localBundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, subfragment).addToBackStack(subfragment.getTag());
fragmentTransaction.commit();
}
感谢您的帮助:)
您正在通过以下方式添加您的 subfragment
:
fragmentTransaction.replace(R.id.fragment_container, subfragment)
现在,请致电:
getFragmentManager().findFragmentById(R.id.fragment_container);
只会 return 你的 subfragment
。因此,您将获得类广播异常