-2

Hey i want to create and show framelayouts in my android appliaction without a xml layout. For this I work with Rect and Fragments. It works fine if all in the MainActivity but if I try to execute in a extra class I get the Error Activity has been destroyed.

I want create rect and inside this I want to load a pdf viewer as fragment.

Here Is My MainActiticy without load xml Layout:

@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);


    }

The CordovaView Class. Here must create the Views and load the File inside.

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

My Error:

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)
...
...
Tarasov
  • 3,625
  • 19
  • 68
  • 128
  • [You asked the same question yesterday](http://stackoverflow.com/questions/42273971/how-i-can-fix-the-error-activity-has-been-destroyed-in-my-class-with-getsupp) ... and **you get an answer** .. **it's about operator `new` and `Activity` derived class** ... please, do not repost the question – Selvin Feb 17 '17 at 09:00
  • this not help me sry. – Tarasov Feb 17 '17 at 09:07
  • i delete the other question because i try to ask better – Tarasov Feb 17 '17 at 09:08

1 Answers1

1
  • CordovaView has incorrect name as it doesn't extend View but extends FrameActivity - this is somewhat misleading. Rename to CordovaActivity.
  • MainActivity.thisContext = this; and MainActivity.thisActivity = (Activity) this; - these are static references to your activity? Don't use static references to your activity ever. Even if they are not public (if they are public - remove public and remove static). And as you don't need reference to your activity inside that same activity - remove that reference at all and use this everywhere you need it.
    You might have some instance of activity in such references, but Activity can be already destroyed - so you can't rely on such references anyway, so you'd better remove them and find out other ways to do what you want.
  • So, you create new Activity using new and then call some method on that activity? Then you finally have to read docs how to start Activity: https://developer.android.com/training/basics/firstapp/starting-activity.html
    You have created instance of Activity, but it is not started in your case. And when you try to attach some fragment to your Activity (which is not started) - of course you have to expect a bunch of errors. To launch your activity one MUST send Intent to system.
    Instead of passing params via constructor you have to pass these params in Intent's extras.
krossovochkin
  • 12,030
  • 7
  • 31
  • 54