这意味着在我的Android碎片应用中破坏了误差活动



嘿,我想在没有XML布局的情况下创建和显示Android Appliauts中的Framelayout。为此,我以矩形和碎片工作。如果全部在主动脉中,它都可以正常工作,但是如果我尝试在额外的类中执行我获得错误活动已被破坏。

我想创建RECT,在此内,我想将PDF查看器作为片段加载。

这是我的Mainactiticy,没有负载XML布局:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainActivity.thisContext = this;
        MainActivity.thisActivity = (Activity) this;
        final PSPDFConfiguration configuration = new PSPDFConfiguration.Builder(BuildConfig.PSPDFKIT_LICENSE_KEY).build();
        final Uri documentUri = Uri.parse("file:///android_asset/psp.pdf");
        CordovaView viewer = new CordovaView(thisContext,10,10,500,500);
        viewer.loadFileInView(thisActivity,thisContext,0,300,configuration,documentUri);

    }

Cordovaview类。这里必须创建视图并加载文件内部。

public class CordovaView extends FragmentActivity {
    private List<WebView>views;
    public CordovaView(Context ctx,int x,int y,int w, int h){
        this.views = new ArrayList<WebView>();
        createView(ctx,x,y,w,h);
    }
    public void createView(Context ctx, int x, int y, int w, int h){
        final Rect rect = new Rect(x, y, x + w, y + h);
        View view = new WebView(ctx);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right - rect.left, rect.bottom - rect.top);
        params.leftMargin = rect.left;
        params.topMargin = rect.top;
        params.gravity = 0;
        view.setLayoutParams(params);
        view.setBackgroundColor(Color.BLUE);
        views.add((WebView) view);
    }
    public void showViewById(Activity ac, int index){
        WebView view = views.get(index);
        ac.addContentView(view,view.getLayoutParams());
    }
    public void loadFileInView(Activity ac, Context ctx, int index, int containerId, final PSPDFConfiguration configuration, Uri documentUri){
        FrameLayout layout = new FrameLayout(ctx);
        layout.setId(containerId);
        WebView view = views.get(index);
        layout.setLayoutParams(view.getLayoutParams());
        view.addView(layout);
        PSPDFFragment fragment = (PSPDFFragment) getSupportFragmentManager().findFragmentById(layout.getId());
//Here I have a error I think :( 
        if(fragment==null){
                fragment = PSPDFFragment.newInstance(documentUri,configuration);
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(layout.getId(),fragment)
                        .commit();
        }
        showViewById(ac,index);
    }
}

我的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: de.wladimir.tarasov.pdfframelayout, PID: 9932
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{de.wladimir.tarasov.pdfframelayout/de.wladimir.tarasov.pdfframelayout.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564)
                      at android.app.ActivityThread.access$800(ActivityThread.java:170)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:194)
                      at android.app.ActivityThread.main(ActivityThread.java:5576)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
                   Caused by: java.lang.IllegalStateException: Activity has been destroyed
                      at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1854)
                      at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:643)
                      at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:603)
                      at de.wladimir.tarasov.pdfframelayout.CordovaView.loadFileInView(CordovaView.java:82)
...
...
  • CordovaView的名称不正确,因为它没有扩展View,而是扩展了FrameActivity-这有些误导。重命名为CordovaActivity
  • MainActivity.thisContext = this;MainActivity.thisActivity = (Activity) this;-这些是对您活动的静态引用吗?不要对您的活动使用静态引用。即使他们不是公开的(如果他们是公开的话 - 删除公开并删除静态)。而且,由于您不需要参考您在同一活动中的活动 - 完全删除该参考并在需要的地方使用this
    您可能会在此类参考中有一些活动的实例,但是活动可能已经破坏了 - 因此您无论如何都不能依靠此类参考,因此您最好将其删除并找出其他方法来完成您想要的事情。
  • 因此,您使用new创建新活动,然后在该活动上调用一些方法?然后,您最终必须阅读文档如何开始活动:https://developer.android.com/training/basics/firstapp/starting-activity.html
    您已经创建了活动实例,但是在您的情况下尚未开始。而且,当您尝试将一些片段附加到您的活动(未开始)时 - 当然,您必须期望很多错误。要启动您的活动一个必须Intent发送到系统。
    而不是通过构造函数传递参数,而必须在意图的附加内容中传递这些参数。

最新更新