Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

How to call an activity from a Dialogfragment in android?

I want to call an activity from Dialogfragment, I have attached the code and logcat below for your reference on what I have tried.Kindly provide me your knowledge on it. Thank you.

 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$3.onCommandFinished(LogDialog.java:75) 02-12 13:47:17.345: E/AndroidRuntime(670): at com.firstadvantage.buisnesslayer.commands.Command$3.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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two ways to call from Fragment to Activity that hosts the Fragment:

  1. Simply casting to HostActivity

    ((HostActivity) getActivity()).methodInActivity();
    
  2. Use interface in Fragment as listener, HostActivity implements the listener:

    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);
    

Hope this is clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...