2017-08-14 75 views
-3

error image 這是錯誤信息片段必須是一個公共靜態類從實例狀態正確地重新創建

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hkmedical.hkmedical/com.hkmedical.hkmedical.pdf.DocumentActivity}: java.lang.IllegalStateException: Fragment com.hkmedical.hkmedical.pdf.DocumentActivity.PlaceholderFragment must be a public static class to be properly recreated from instance state. 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504) 
                     at android.app.ActivityThread.access$900(ActivityThread.java:165) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:150) 
                     at android.app.ActivityThread.main(ActivityThread.java:5546) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:682) 
                     Caused by: java.lang.IllegalStateException: Fragment com.hkmedical.hkmedical.pdf.DocumentActivity.PlaceholderFragment must be a public static class to be properly recreated from instance state. 
                     at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:384) 
                     at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:369) 
                     at com.hkmedical.hkmedical.pdf.DocumentActivity.onCreate(DocumentActivity.java:55) 
                     at android.app.Activity.performCreate(Activity.java:6367) 
                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397) 
                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2504)  
                     at android.app.ActivityThread.access$900(ActivityThread.java:165)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:150)  
                     at android.app.ActivityThread.main(ActivityThread.java:5546)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:682)  


in the activity, when onCreate, the runtime exception appeared. 
fragment = new PlaceholderFragment(this); 
    if (savedInstanceState == null) { 
     this.getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, fragment).commit(); 
    } 

這裏是哲內片段

@SuppressLint("ValidFragment") 
public class PlaceholderFragment extends Fragment { 
    private Context mContext; 

    public PlaceholderFragment(Context context) { 
     mContext = context; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = new View(mContext); 
     rootView.setBackgroundColor(Color.GRAY); 
     mDoc = new SPDocument(); 
     long lResult = mDoc.open(mFilePath); 
     if (lResult == 0) { 
      drawView(); 
     } 
     return rootView; 
    } 

    public void drawView() { 
     mReaderView = new SPReaderViews(mContext, mDoc); 
     mView = mReaderView.showDocument(); 
     LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.MATCH_PARENT); 
     getActivity().addContentView(mReaderView, params); 
     mView.goToPage(0);} 

這是我的gradle產出:

compileSdkVersion 25 
buildToolsVersion '25.0.0' 

我想知道如何使它工作好,我想知道如何使我工作得很好,我想知道如何使它工作得很好。 但是zhe代碼在我的其他項目中效果很好。

我想知道如何使它工作良好,我想知道如何使它工作得很好,我想知道如何使它工作良好。 但是zhe代碼在我的其他項目中效果很好。

我想知道如何使它工作良好,我想知道如何使它工作得很好,我想知道如何使它工作良好。 但是zhe代碼在我的其他項目中效果很好。

+0

將「添加」更改爲「替換」 –

+0

片段應該只有空構造函數。 –

回答

0

問題是片段應該有一個空的構造函數。如果您的應用程序將被Android系統殺死,並且您再次打開它,Android會嘗試恢復活動和碎片的最後狀態。在這種情況下,它將使用默認的構造函數,因此所有傳遞給構造函數的東西都將丟失。如果你需要傳遞數據來分割,你應該使用fragment.setArguments()。如果需要獲取片段中的上下文,則可以使用getActivity()getContext()

相關問題