ListFragment Cannot be cast to android.app.Activity -
facing error says my listfragment cannot cast android.app.activity. it's tablet app 2 pane layout , left side. getting crash not sure why. awesome.
code:
import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.app.listfragment; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; public class mainactivitytab extends listfragment { fragmentmanager fm; fragmenttransaction ft; fragment myfragment; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); string[] values = new string[] { "player", "", "about this", "the science", "information", "instructions", "pdf"}; arrayadapter<string> adapter = new arrayadapter<string>(getactivity(), android.r.layout.simple_list_item_1, values); setlistadapter(adapter); fragmentmanager fm = getactivity().getsupportfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); if (savedinstancestate == null){ fragment_11 myfragment = new fragment_11(); ft.add(r.id.content_layout, myfragment); ft.commit(); }} @override public void onlistitemclick(listview l, view v, int position, long id){ fragment newfragment = null; fragmentmanager fm = getactivity().getsupportfragmentmanager(); switch(position){ case 0: newfragment = new fragment_11(); break; case 1: newfragment = new fragment_2(); break; case 2: newfragment = new fragment_3(); break; case 3: newfragment = new fragment_4(); break; case 4: newfragment = new fragment_5(); break; case 5: newfragment = new fragment_6(); break; } fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.content_layout, newfragment); ft.addtobackstack(null) .commit(); } }
error:
09-04 12:07:24.897: e/androidruntime(32209): fatal exception: main 09-04 12:07:24.897: e/androidruntime(32209): java.lang.runtimeexception: unable instantiate activity componentinfo{com.appexample.sseconds/com.appexample.sseconds.mainactivitytab}: java.lang.classcastexception: com.appexample.sseconds.mainactivitytab cannot cast android.app.activity 09-04 12:07:24.897: e/androidruntime(32209): @ android.app.activitythread.performlaunchactivity(activitythread.java:2137) 09-04 12:07:24.897: e/androidruntime(32209): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2261) 09-04 12:07:24.897: e/androidruntime(32209): @ android.app.activitythread.access$600(activitythread.java:141) 09-04 12:07:24.897: e/androidruntime(32209): @ android.app.activitythread$h.handlemessage(activitythread.java:1256) 09-04 12:07:24.897: e/androidruntime(32209): @ android.os.handler.dispatchmessage(handler.java:99) 09-04 12:07:24.897: e/androidruntime(32209): @ android.os.looper.loop(looper.java:137) 09-04 12:07:24.897: e/androidruntime(32209): @ android.app.activitythread.main(activitythread.java:5103) 09-04 12:07:24.897: e/androidruntime(32209): @ java.lang.reflect.method.invokenative(native method) 09-04 12:07:24.897: e/androidruntime(32209): @ java.lang.reflect.method.invoke(method.java:525) 09-04 12:07:24.897: e/androidruntime(32209): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 09-04 12:07:24.897: e/androidruntime(32209): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 09-04 12:07:24.897: e/androidruntime(32209): @ dalvik.system.nativestart.main(native method) 09-04 12:07:24.897: e/androidruntime(32209): caused by: java.lang.classcastexception: com.appexample.sseconds.mainactivitytab cannot cast android.app.activity 09-04 12:07:24.897: e/androidruntime(32209): @ android.app.instrumentation.newactivity(instrumentation.java:1061) 09-04 12:07:24.897: e/androidruntime(32209): @ android.app.activitythread.performlaunchactivity(activitythread.java:2128) 09-04 12:07:24.897: e/androidruntime(32209): ... 11 more
don't know crash, doing in wrong manner. it's bad practice start fragmenttransaction on activity inside fragment code. transaction manipulations in extends activity class. if need onlistitemclick events - can do
((myactivity) getactivity()).onlistitemclick()
to deliver event.
Comments
Post a Comment