1

我正在使用 ActionBarSherlock 来显示几个片段。其中一个片段有一个调用另一个活动的按钮。在应用程序强制关闭之前,单击该按钮会暂时显示新活动。我已经尝试覆盖 onSaveInstanceState() 方法。

登录.java

public class login extends SherlockFragment implements OnClickListener {
Button login;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.login, container, false);


    login = (Button) view.findViewById(R.id.btnLogin);
    login.setOnClickListener(this);

    return view;
    }

@Override
public void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    outState.putString("DO NOT CRASH", "OK");
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){

    case R.id.btnLogin : Intent i = new Intent(getActivity(), success.class);
                            startActivity(i);
                            break;

    }
}

}

这是它调用的简单活动。

成功.java

public class success extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.success);
}

}

logcat 错误信息:

06-17 10:33:24.276: E/AndroidRuntime(13546): FATAL EXCEPTION: main
06-17 10:33:24.276: E/AndroidRuntime(13546): java.lang.NullPointerException
06-17 10:33:24.276: E/AndroidRuntime(13546): at  android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at  android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.app.Activity.performSaveInstanceState(Activity.java:1147)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1216)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3129)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3188)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.app.ActivityThread.access$900(ActivityThread.java:141)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.os.Looper.loop(Looper.java:137)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at java.lang.reflect.Method.invokeNative(Native Method)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at java.lang.reflect.Method.invoke(Method.java:511)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-17 10:33:24.276: E/AndroidRuntime(13546):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您只需替换成功将活动扩展到 FragmentActivity。

于 2013-06-17T07:06:11.757 回答