0
我將從第一個片段到第二個。當我旋轉或最小化第二個片段上的屏幕時,應用程序崩潰。早些時候,當我用最小化的應用程序第一個片段被稱爲(不知道爲什麼),現在它的崩潰這裏是代碼:旋轉屏幕或最小化應用程序時,碎片崩潰
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)
這是我的全力主要活動代碼:
@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();
}
感謝您的幫助:)
您是否可以分享您的主要活動代碼?或者至少80號和74號線? – tpk
就ClassCastException而言,它意味着你正在聲明某種類型的變量並將其分配給在佈局xml文件中定義的另一種類型...... –
@mr_tkp完整主要活動代碼添加 –