ClassCastException: fragment cannot be cast to android.app.activtity -
i have class extends fragment , there button on once click initialises new class extends activity.. wanted pass data(e.g. string) on button click activity caller fragment.
firstactivity extends fragment
on button click
intent startcontact = new intent(getactivity(), contact.class); getactivity().startactivityforresult(startcontact,1);
contact extends activity
on button click
bundle bundle = new bundle(); bundle.putstring("listofcontacts", tapo1); intent intent = new intent(contact.this, first_activity.class); intent.putextras(bundle); setresult(result_ok); startactivity(intent); finish();
firstactivity onactivityresult
@override public void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub super.onactivityresult(requestcode, resultcode, data); multiautocompletetextview txtphoneno = (multiautocompletetextview) getactivity().findviewbyid(r.id.txtphoneno); string strtxt = null; if (requestcode == 1) { if(resultcode == getactivity().result_ok) { strtxt=data.getstringextra("listofcontacts"); txtphoneno.settext(strtxt); } } }
when tried click button contact class got error:
classcastexception: firstactivity cannot cast android.app.activity
pls help!
logcat:
08-15 23:26:39.153: e/androidruntime(21267): fatal exception: main 08-15 23:26:39.153: e/androidruntime(21267): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.john/com.example.john.first_activity}: java.lang.classcastexception: com.example.john.first_activity cannot cast android.app.activity 08-15 23:26:39.153: e/androidruntime(21267): @ android.app.activitythread.performlaunchactivity(activitythread.java:2001) 08-15 23:26:39.153: e/androidruntime(21267): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2104) 08-15 23:26:39.153: e/androidruntime(21267): @ android.app.activitythread.access$600(activitythread.java:134) 08-15 23:26:39.153: e/androidruntime(21267): @ android.app.activitythread$h.handlemessage(activitythread.java:1247) 08-15 23:26:39.153: e/androidruntime(21267): @ android.os.handler.dispatchmessage(handler.java:99) 08-15 23:26:39.153: e/androidruntime(21267): @ android.os.looper.loop(looper.java:154) 08-15 23:26:39.153: e/androidruntime(21267): @ android.app.activitythread.main(activitythread.java:4624) 08-15 23:26:39.153: e/androidruntime(21267): @ java.lang.reflect.method.invokenative(native method) 08-15 23:26:39.153: e/androidruntime(21267): @ java.lang.reflect.method.invoke(method.java:511) 08-15 23:26:39.153: e/androidruntime(21267): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:809) 08-15 23:26:39.153: e/androidruntime(21267): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:576) 08-15 23:26:39.153: e/androidruntime(21267): @ dalvik.system.nativestart.main(native method) 08-15 23:26:39.153: e/androidruntime(21267): caused by: java.lang.classcastexception: com.example.john.first_activity cannot cast android.app.activity 08-15 23:26:39.153: e/androidruntime(21267): @ android.app.instrumentation.newactivity(instrumentation.java:1023) 08-15 23:26:39.153: e/androidruntime(21267): @ android.app.activitythread.performlaunchactivity(activitythread.java:1992) 08-15 23:26:39.153: e/androidruntime(21267): ... 11 more
you using onactivityresult in fragment firstactivity.class activity class can use method , receive result code, not fragment. can use flags check change in value , check in onresume() in fragment firstactivity.class
Comments
Post a Comment