【问题标题】:What mean the error Activity has been destroyed in my android fragment application?错误 Activity 已在我的 android 片段应用程序中被破坏是什么意思?
【发布时间】:2017-02-17 08:56:11
【问题描述】:

嘿,我想在没有 xml 布局的情况下在我的 android 应用程序中创建和显示框架布局。为此,我使用 Rect 和 Fragments。如果所有都在 MainActivity 中它工作正常,但如果我尝试在额外的类中执行,我得到错误 Activity 已被破坏。

我想创建 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 类。这里必须创建Views并加载里面的File。

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)
...
...

【问题讨论】:

  • You asked the same question yesterday ... 和 你会得到答案 .. 这是关于运算符 newActivity 派生类 ...请不要转发问题
  • 这帮不了我。
  • 我删除了另一个问题,因为我想问得更好

标签: java android android-activity fragment android-framelayout


【解决方案1】:
  • CordovaView 的名称不正确,因为它没有扩展 View 而是扩展了 FrameActivity - 这有点误导。重命名为CordovaActivity
  • MainActivity.thisContext = this;MainActivity.thisActivity = (Activity) this; - 这些是对您活动的静态引用?永远不要对您的活动使用静态引用。即使它们不是公开的(如果它们是公开的 - 删除公共并删除静态)。由于您不需要在同一个活动中引用您的活动 - 完全删除该引用并在您需要的任何地方使用this
    您可能在此类引用中有一些 Activity 实例,但 Activity 可能已经被销毁 - 因此您无论如何都不能依赖此类引用,因此您最好删除它们并找出其他方法来做您想做的事。
  • 那么,您使用new 创建新活动,然后对该活动调用一些方法?然后你最后必须阅读文档如何启动 Activity:https://developer.android.com/training/basics/firstapp/starting-activity.html
    您已经创建了 Activity 实例,但在您的情况下它没有启动。当您尝试将一些片段附加到您的活动(未启动)时 - 当然您必须期待一堆错误。要启动您的活动,必须Intent 发送到系统。
    您必须在 Intent 的附加组件中传递这些参数,而不是通过构造函数传递参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2019-07-21
    • 2011-09-02
    相关资源
    最近更新 更多