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);}
这是我的傻瓜:
compileSdkVersion 25
buildToolsVersion '25.0.0'
我想知道如何让它运作良好,我想知道如何让它运作良好,我想知道如何让它运作良好。 但是代码在我的其他项目中效果很好。
我想知道如何让它运作良好,我想知道如何让它运作良好,我想知道如何让它运作良好。 但是代码在我的其他项目中效果很好。
我想知道如何让它运作良好,我想知道如何让它运作良好,我想知道如何让它运作良好。 但是代码在我的其他项目中效果很好。
答案 0 :(得分:1)
问题是片段应该有一个空构造函数。如果您的应用程序将被Android系统杀死并再次打开它,Android将尝试恢复活动和片段的最后状态。在这种情况下,它将使用默认构造函数,因此传递给构造函数的所有内容都将丢失。如果需要将数据传递给片段,则应使用fragment.setArguments()
。如果您需要获取片段中的上下文,可以使用getActivity()
或getContext()