我有一个延伸到片段的类,一旦点击它就会初始化一个延伸到活动的新类。我想在按钮点击时将数据(例如字符串)从该活动传递回调用者片段。
FirstActivity 扩展片段
点击按钮
Intent startcontact = new Intent(getActivity(), Contact.class);
getActivity().startActivityForResult(startcontact,1);
联系扩展活动
点击按钮
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);
}
}
}
当我尝试单击联系人类中的按钮时,出现错误:
ClassCastException: FirstActivity cannot be cast to android.app.activity
请帮忙!
日志猫:
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