2017-05-03 75 views
-1

我有一個應用程序列表,當一個項目被點擊時第二個活動 打開一個可滾動的界面,其中有一個片段。沒有 加載片段應用程序工作正常,但是當我想加載 片段它停止。Android應用程序在打開片段時停止

DiveDetailsFragment.Java

package be.gillescoeman.scubajournal; 

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
// import android.app.Fragment; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 


/** 
    * A simple {@link Fragment} subclass. 
    * Activities that contain this fragment must implement the 
    * {@link DiveDetailsFragment.OnFragmentInteractionListener} interface 
    * to handle interaction events. 
    * Use the {@link DiveDetailsFragment#newInstance} factory method to 
    * create an instance of this fragment. 
    */ 
public class DiveDetailsFragment extends Fragment { 
    private OnFragmentInteractionListener mListener; 

    public DiveDetailsFragment() { 
     // Required empty public constructor 
    } 

    public static DiveDetailsFragment newInstance() { 
     DiveDetailsFragment fragment = new DiveDetailsFragment(); 
     return fragment; 
    } 

    @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_dive_details, container, false); 
    } 

    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

DiveDetailsActivity.Java

package be.gillescoeman.scubajournal; 

// import android.app.Fragment; 
// import android.app.FragmentTransaction; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 

public class DiveDetailsActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_dive_details); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

//  FragmentTransaction ft = getFragmentManager().beginTransaction(); 
//  Fragment diveDetailsFragment = DiveDetailsFragment.newInstance(); 
//  ft.add(R.id.diveDetailsFragmentContainer, diveDetailsFragment); 
//  ft.commit(); 

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

//  if (savedInstanceState == null) { 
//   getSupportFragmentManager() 
//     .beginTransaction() 
//     .add(R.id.diveDetailsFragmentContainer, DiveDetailsFragment.newInstance(), "DiveDetails") 
//     .commit(); 
//  } 

     if (findViewById(R.id.diveDetailsFragmentContainer) != null) { 
      if (savedInstanceState != null) { 
       return; 
      } 

      // Create an instance of AddServerFragment 
      DiveDetailsFragment fragment = new DiveDetailsFragment(); 

      // Add the fragment to the 'fragment_container' FrameLayout 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.diveDetailsFragmentContainer, fragment).commit(); 
     } 
    } 
} 

fragment_dive_details.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    tools:context="be.gillescoeman.scubajournal.DiveDetailsFragment" 
    class="be.gillescoeman.scubajournal.DiveDetailsFragment"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/text_margin" 
     android:text="@string/large_text" /> 

</FrameLayout> 

content_dive_details.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="be.gillescoeman.scubajournal.DiveDetailsActivity" 
    tools:showIn="@layout/activity_dive_details"> 

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/diveDetailsFragmentContainer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</android.support.v4.widget.NestedScrollView> 

activity_dive_detail.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="be.gillescoeman.scubajournal.DiveDetailsActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/app_bar_height" 
     android:fitsSystemWindows="true" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_dive_details" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     app:layout_anchor="@id/app_bar" 
     app:layout_anchorGravity="bottom|end" 
     app:srcCompat="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

Android的監控日誌

05-03 13:08:55.049 16815-16815/? I/art: Not late-enabling -Xcheck:jni (already on) 
05-03 13:08:55.049 16815-16815/? W/art: Unexpected CPU variant for X86 using defaults: x86 
05-03 13:08:55.189 16815-16815/be.gillescoeman.scubajournal W/System: ClassLoader referenced unknown path: /data/app/be.gillescoeman.scubajournal-2/lib/x86 
05-03 13:08:55.198 16815-16815/be.gillescoeman.scubajournal I/InstantRun: starting instant run server: is main process 
05-03 13:08:55.254 16815-16815/be.gillescoeman.scubajournal W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
05-03 13:08:55.609 16815-16822/be.gillescoeman.scubajournal W/art: Suspending all threads took: 13.168ms 
05-03 13:08:55.859 16815-16843/be.gillescoeman.scubajournal I/OpenGLRenderer: Initialized EGL, version 1.4 
05-03 13:08:55.859 16815-16843/be.gillescoeman.scubajournal D/OpenGLRenderer: Swap behavior 1 
05-03 13:08:55.859 16815-16843/be.gillescoeman.scubajournal W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 
05-03 13:08:55.859 16815-16843/be.gillescoeman.scubajournal D/OpenGLRenderer: Swap behavior 0 
05-03 13:08:55.965 16815-16815/be.gillescoeman.scubajournal W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
05-03 13:09:00.502 16815-16815/be.gillescoeman.scubajournal D/AndroidRuntime: Shutting down VM 

--------- beginning of crash 05-03 13:09:00.502 16815-16815/be.gillescoeman.scubajournal E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: be.gillescoeman.scubajournal, PID: 16815 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{be.gillescoeman.scubajournal/be.gillescoeman.scubajournal.DiveDetailsActivity}: java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
Caused by: java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener 
    at be.gillescoeman.scubajournal.DiveDetailsFragment.onAttach(DiveDetailsFragment.java:57) 
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1231) 
    at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1085) 
    at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976) 
    at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95) 
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146) 
    at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) 
    at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607) 
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248) 
    at android.app.Activity.performStart(Activity.java:6696) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
    at android.app.ActivityThread.-wrap12(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:154)  
    at android.app.ActivityThread.main(ActivityThread.java:6119)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
+0

發表您的錯誤日誌也 –

+0

後異常日誌,它有助於回答在這裏需要查詢 –

+0

錯誤日誌.. – ZeroOne

回答

0

實際刪除onAttach()onDetach()方法似乎這樣的伎倆

1

Fragment以下行假定活動它在哪裏被連接將執行OnFragmentInteractionListener接口。

mListener = (OnFragmentInteractionListener) context; 

你需要改變:

public class DiveDetailsActivity extends AppCompatActivity { 

到:

public class DiveDetailsActivity extends AppCompatActivity implements OnFragmentInteractionListener { 

並實現所有抽象方法中OnFragmentInteractionListener,可這些方法是空白,都不會有問題爲這個問題。

相關問題