android - Activity is being destroyed on replacing Fragment -
android - Activity is being destroyed on replacing Fragment -
i new android , trying write simple dummy app larn creating own view classes , on. why doing this? wanted have ui consists of several pages can navigated through pressing buttons on action bar. wanted add together feature of navigating via touch gestures. faced problem trying solve dummy app, same original app, bit simpler.
the (former) problem: fragments have scrollview on 1 side, eats touch gesture, when click on them.
i read , figured out have create own framelayout , @override onintercepttouchevent , ontouchevent methods. utilize new framelayout fragment container. yet it's still not working when test app scrollview in fragmentlayout instead of linearlayout(as in codesample). @ point not biggest problem, think have work bit on 2 methods them work want.
at point app crashes when fragments beingness swapped via touch gestures... buttons still work.
i getting error(logcat):
06-18 14:34:03.295: w/dalvikvm(2983): threadid=1: thread exiting uncaught exception (group=0x409f51f8) 06-18 14:34:03.305: e/androidruntime(2983): fatal exception: main 06-18 14:34:03.305: e/androidruntime(2983): java.lang.illegalstateexception: activity has been destroyed 06-18 14:34:03.305: e/androidruntime(2983): @ android.app.fragmentmanagerimpl.enqueueaction(fragmentmanager.java:1280) 06-18 14:34:03.305: e/androidruntime(2983): @ android.app.backstackrecord.commitinternal(backstackrecord.java:541) 06-18 14:34:03.305: e/androidruntime(2983): @ android.app.backstackrecord.commit(backstackrecord.java:525) 06-18 14:34:03.305: e/androidruntime(2983): @ com.example.trysomething.mainactivity.switchfragment(mainactivity.java:97) 06-18 14:34:03.305: e/androidruntime(2983): @ com.example.trysomething.mainactivity.navforward(mainactivity.java:74)
this mainactivity:
public class mainactivity extends activity { int pos; touchinterceptinglayout mtil; charsequence mtitle; string[] mtitles; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtil = (touchinterceptinglayout) findviewbyid(r.id.fragment_container); pos = 0; mtitle = gettitle(); mtitles = getresources().getstringarray(r.array.dummy_seiten); if(savedinstancestate == null){ switchfragment(0); } } @override public boolean oncreateoptionsmenu(menu menu){ menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main, menu); homecoming super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item){ switch(item.getitemid()){ case r.id.action_nav_back: navbackwards(); break; case r.id.action_nav_forward: navforward(); break; } homecoming super.onoptionsitemselected(item); } public void settitle(charsequence title){ mtitle = title; getactionbar().settitle(mtitle); } public void navforward(){ if(pos == 2){ pos = 0; switchfragment(pos); } else { pos += 1; switchfragment(pos); } } public void navbackwards(){ if(pos == 0){ pos = 2; switchfragment(pos); } else { pos -= 1; switchfragment(pos); } } public void switchfragment(int position){ fragment newfrag; bundle args = new bundle(); args.putint(dummyfragmentsimpleview.arg_position, position); newfrag = new dummyfragmentsimpleview(); newfrag.setarguments(args); android.app.fragmentmanager fm = getfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fragment_container, newfrag); ft.commit(); } }
this dummyfragmentsimpleview (java):
public class dummyfragmentsimpleview extends fragment { public static string arg_position = "position"; bundle args; string[] dummystringarray; string[] nonsensearray; layoutparams params; public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); dummystringarray = getresources().getstringarray(r.array.dummy_array); nonsensearray = getresources().getstringarray(r.array.nonsense_array); params = new layoutparams(layoutparams.match_parent, layoutparams.wrap_content); args = getarguments(); } public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view mysimplefrag = inflater.inflate(r.layout.fragment_notscrolling, container, false); imageview image = (imageview) mysimplefrag.findviewbyid(r.id.image_container); string bild = getresources().getstringarray(r.array.dummy_seiten)[args.getint(arg_position)]; int bildid = getresources().getidentifier(bild.tolowercase(locale.getdefault()), "drawable", getactivity().getpackagename()); ((imageview) image).setimageresource(bildid); linearlayout dummylayout = (linearlayout) mysimplefrag.findviewbyid(r.id.content_container); textview t = (textview) mysimplefrag.findviewbyid(r.id.nonsense_text); t.settext(nonsensearray[args.getint(arg_position)]); for(int = 0; < dummystringarray.length; i++){ textview text = new textview(getactivity()); text.settext(dummystringarray[i]); text.setid(i); text.setlayoutparams(params); text.settextsize(20); ((linearlayout) dummylayout).addview(text); } getactivity().settitle(bild); homecoming mysimplefrag; } }
this xml simple view:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:baselinealigned="false"> <linearlayout android:id="@+id/left_panel" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#c00000" android:gravity="center"> <imageview android:id="@+id/image_container" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <linearlayout android:id="@+id/right_panel" android:layout_weight="2" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical"> <textview android:id="@+id/nonsense_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textsize="30sp" android:textstyle="bold"/> <linearlayout android:id="@+id/content_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"/> </linearlayout> </linearlayout>
and touchinterceptinglayout class:
public class touchinterceptinglayout extends framelayout{ boolean misnavigating = false; motioneventhandler handler = new motioneventhandler(); public touchinterceptinglayout(context context) { super(context); } public touchinterceptinglayout(context context, attributeset attrs) { this(context, attrs, 0); } public touchinterceptinglayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); } @override protected void onlayout(boolean changed, int left, int top, int right, int bottom) { for(int = 0; < getchildcount(); i++){ getchildat(i).layout(left, top, right, bottom); } } @override public boolean onintercepttouchevent(motionevent event){ float x1 = 0; float x2 = 0; final int action = event.getaction(); if(action == motionevent.action_cancel || action == motionevent.action_up){ misnavigating = false; homecoming false; } switch (action){ case motionevent.action_down: x1 = event.getx(); break; case motionevent.action_move: homecoming false; case motionevent.action_up: x2 = event.getx(); if(math.abs(x2 - x1) > 150){ misnavigating = true; homecoming true; } else { homecoming false; } } homecoming false; } @override public boolean ontouchevent(motionevent event){ switch(event.getaction()){ case motionevent.action_down: handler.setxone(event.getx()); homecoming true; case motionevent.action_up: handler.setxtwo(event.getx()); handler.setdelta(); handler.performaction(); break; } homecoming super.ontouchevent(event); } }
motioneventhandler class:
public class motioneventhandler { static final int min_x_distance = 150; public float x1 = 0; public float x2 = 0; public float deltax = 0; mainactivity myboss = new mainactivity(); public motioneventhandler(){ } public void setxone(float x){ x1 = x; } public void setxtwo(float x){ x2 = x; } public float getxone(){ homecoming x1; } public float getxtwo(){ homecoming x2; } public void setdelta(){ deltax = x2 - x1; } public float getdelta(){ homecoming deltax; } public boolean ispagebeingswapped(){ if (math.abs(deltax)<min_x_distance){ homecoming false; } else { homecoming true; } } public void performaction(){ if(ispagebeingswapped()){ if(deltax < 0){ myboss.navbackwards(); } else { myboss.navforward(); } } } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <com.example.trysomething.touchinterceptinglayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
i hope can help, , problem description not confusing, , problem not dumb... ;s
thanks in advance!
your problem occurs right here in motioneventhandler
public class motioneventhandler { static final int min_x_distance = 150; public float x1 = 0; public float x2 = 0; public float deltax = 0; // -- right here -- mainactivity myboss = new mainactivity(); // rest of code }
the error getting misleading because it's not mainactivity
has been destroyed. issue 1 using isn't 1 visible on screen. in every case, never want instantiate activity
calling constructor.
fortunately, prepare relatively easy. motioneventhandler
should have constructor takes in mainactivity
following:
public motioneventhandler(mainactivity mainact) { myboss = mainact; }
and in touchinterceptinglayout
should instantiate handler within constructors.
public class touchinterceptinglayout extends framelayout{ motioneventhandler handler; public touchinterceptinglayout(context context) { super(context); if(context instanceof mainactivity) { handler = = new motioneventhandler((mainactivity) context) } else { // not valid context } } public touchinterceptinglayout(context context, attributeset attrs) { this(context, attrs, 0); } public touchinterceptinglayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); if(context instanceof mainactivity) { handler = = new motioneventhandler((mainactivity) context) } else { // not valid context } } // rest of code }
android android-layout android-fragments framelayout fragmentmanager
Comments
Post a Comment