I have a class which extends to fragment and there is a button on it once click it initialises a new class which extends to an activity.. i wanted to pass data(e.g. string) on button click from that activity back to the 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 i tried to click the button from the contact class i got an error:
ClassCastException: FirstActivity cannot be cast to 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 to instantiate activity ComponentInfo{com.example.john/com.example.john.First_Activity}: java.lang.ClassCastException: com.example.john.First_Activity cannot be cast to android.app.Activity
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2001)
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.app.ActivityThread.access$600(ActivityThread.java:134)
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.os.Looper.loop(Looper.java:154)
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.app.ActivityThread.main(ActivityThread.java:4624)
08-15 23:26:39.153: E/AndroidRuntime(21267): at java.lang.reflect.Method.invokeNative(Native Method)
08-15 23:26:39.153: E/AndroidRuntime(21267): at java.lang.reflect.Method.invoke(Method.java:511)
08-15 23:26:39.153: E/AndroidRuntime(21267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
08-15 23:26:39.153: E/AndroidRuntime(21267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
08-15 23:26:39.153: E/AndroidRuntime(21267): at 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 be cast to android.app.Activity
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
08-15 23:26:39.153: E/AndroidRuntime(21267): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1992)
08-15 23:26:39.153: E/AndroidRuntime(21267): ... 11 more