如何从 android 中的 Dialogfragment 调用 activity?
How to call an activity from a Dialogfragment in android?
我想从 Dialogfragment 中调用一个 activity,我附上了代码,下面 logcat 供您参考我所拥有的内容 tried.Kindly 向我提供您的知识。谢谢。
Intent intent = new Intent(getActivity(), LinkActivity.class);
getActivity().startActivityForResult(intent, 0);
Logcat:
02-12 13:47:17.345: E/AndroidRuntime(670): FATAL EXCEPTION: main
02-12 13:47:17.345: E/AndroidRuntime(670): java.lang.NullPointerException
02-12 13:47:17.345: E/AndroidRuntime(670): at android.content.ComponentName.(ComponentName.java:75)
02-12 13:47:17.345: E/AndroidRuntime(670): at android.content.Intent.(Intent.java:3122)
02-12 13:47:17.345: E/AndroidRuntime(670): at com.firstadvantage.activities.LogDialog.LinkActivity(LogDialog.java:93)
02-12 13:47:17.345: E/AndroidRuntime(670): at com.firstadvantage.activities.LogDialog.onCommandFinished(LogDialog.java:75)
02-12 13:47:17.345: E/AndroidRuntime(670): at com.firstadvantage.buisnesslayer.commands.Command.run(Command.java:85)
02-12 13:47:17.345: E/AndroidRuntime(670): at android.os.Handler.handleCallback(Handler.java:605)
02-12 13:47:17.345: E/AndroidRuntime(670): at android.os.Handler.dispatchMessage(Handler.java:92)
02-12 13:47:17.345: E/AndroidRuntime(670): at android.os.Looper.loop(Looper.java:137)
02-12 13:47:17.345: E/AndroidRuntime(670): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-12 13:47:17.345: E/AndroidRuntime(670): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 13:47:17.345: E/AndroidRuntime(670): at java.lang.reflect.Method.invoke(Method.java:511)
02-12 13:47:17.345: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-12 13:47:17.345: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-12 13:47:17.345: E/AndroidRuntime(670): at dalvik.system.NativeStart.main(Native Method)
有两种方法可以从 Fragment 调用托管 Fragment 的 Activity:
简单地投射到主机Activity
((HostActivity) getActivity()).methodInActivity();
使用Fragment中的接口作为监听器,HostActivity实现监听器:
private SuperListener hostActivity;
//In Fragment, define interfce
public interface SuperListener{
//for example a confirm dialog
void getDialogOk(View dialogView);
}
//in constructor, get listener instance from HostActivity
public YourDialogFragment(SuperListener hostActivity)
{
this.hostActivity = hostActivity;
}
//when `Ok` clicked
hostActivity.getDialogOk(dialogView);
希望这是清楚的。
我想从 Dialogfragment 中调用一个 activity,我附上了代码,下面 logcat 供您参考我所拥有的内容 tried.Kindly 向我提供您的知识。谢谢。
Intent intent = new Intent(getActivity(), LinkActivity.class);
getActivity().startActivityForResult(intent, 0);
Logcat:
02-12 13:47:17.345: E/AndroidRuntime(670): FATAL EXCEPTION: main 02-12 13:47:17.345: E/AndroidRuntime(670): java.lang.NullPointerException 02-12 13:47:17.345: E/AndroidRuntime(670): at android.content.ComponentName.(ComponentName.java:75) 02-12 13:47:17.345: E/AndroidRuntime(670): at android.content.Intent.(Intent.java:3122) 02-12 13:47:17.345: E/AndroidRuntime(670): at com.firstadvantage.activities.LogDialog.LinkActivity(LogDialog.java:93) 02-12 13:47:17.345: E/AndroidRuntime(670): at com.firstadvantage.activities.LogDialog.onCommandFinished(LogDialog.java:75) 02-12 13:47:17.345: E/AndroidRuntime(670): at com.firstadvantage.buisnesslayer.commands.Command.run(Command.java:85) 02-12 13:47:17.345: E/AndroidRuntime(670): at android.os.Handler.handleCallback(Handler.java:605) 02-12 13:47:17.345: E/AndroidRuntime(670): at android.os.Handler.dispatchMessage(Handler.java:92) 02-12 13:47:17.345: E/AndroidRuntime(670): at android.os.Looper.loop(Looper.java:137) 02-12 13:47:17.345: E/AndroidRuntime(670): at android.app.ActivityThread.main(ActivityThread.java:4424) 02-12 13:47:17.345: E/AndroidRuntime(670): at java.lang.reflect.Method.invokeNative(Native Method) 02-12 13:47:17.345: E/AndroidRuntime(670): at java.lang.reflect.Method.invoke(Method.java:511) 02-12 13:47:17.345: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 02-12 13:47:17.345: E/AndroidRuntime(670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 02-12 13:47:17.345: E/AndroidRuntime(670): at dalvik.system.NativeStart.main(Native Method)
有两种方法可以从 Fragment 调用托管 Fragment 的 Activity:
简单地投射到主机Activity
((HostActivity) getActivity()).methodInActivity();
使用Fragment中的接口作为监听器,HostActivity实现监听器:
private SuperListener hostActivity; //In Fragment, define interfce public interface SuperListener{ //for example a confirm dialog void getDialogOk(View dialogView); } //in constructor, get listener instance from HostActivity public YourDialogFragment(SuperListener hostActivity) { this.hostActivity = hostActivity; } //when `Ok` clicked hostActivity.getDialogOk(dialogView);
希望这是清楚的。