Q:

How to use getIntent and startActivity in Fragment?

The code like the following is in Activity , it will call setAppLocale function.
After call the setAppLocale function , it will finish(); and restart by startActivity(intent);
The code in Activity
setAppLocale(mLocales[i]) ;

Intent intent = getIntent() ;
finish() ;
startActivity(intent) ;

And now , I want to do the same thing in Fragment by using the Button like the following code.
And I have define Activity activity = getActivity(); in this Fragment.
    public void onClick(View v) {
        // TODO Auto-generated method stub
        MainActivity.setAppLocale(mLocales[1]);

        Intent intent = activity.getIntent();
        activity.finish();
        activity.startActivity(intent);
    }

But it crash and the error log is like the following:
D/AndroidRuntime(19694): Shutting down VM

W/dalvikvm(19694): threadid=1: thread exiting with uncaught exception (group=0x416cc450)

--------- beginning of /dev/log/system
E/AndroidRuntime(19694): FATAL EXCEPTION: main
E/AndroidRuntime(19694): java.lang.NullPointerException
E/AndroidRuntime(19694):    at tw.com.a_i_t.IPCamViewer.Control.LanguageSettings$3.onClick(LanguageSettings.java:85)
E/AndroidRuntime(19694):    at android.view.View.performClick(View.java:4147)
E/AndroidRuntime(19694):    at android.view.View$PerformClick.run(View.java:17161)
E/AndroidRuntime(19694):    at android.os.Handler.handleCallback(Handler.java:615)
E/AndroidRuntime(19694):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(19694):    at android.os.Looper.loop(Looper.java:213)
E/AndroidRuntime(19694):    at android.app.ActivityThread.main(ActivityThread.java:4786)
E/AndroidRuntime(19694):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(19694):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(19694):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
E/AndroidRuntime(19694):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
E/AndroidRuntime(19694):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  568):   Force finishing activity tw.com.a_i_t.IPCamViewer/.MainActivity

The code at LanguageSettings.java:85 is Intent intent = activity.getIntent();
How to solve this problem?

A:

do this in onActivityCreated
Activity activity = getActivity();

The reference to the activity is only available after the call of onActivityCreated so calling getActivity() after the call of this function will not return null.

