是否可以在没有触发按钮的情况下从片段中打开新活动?

时间:2014-06-14 03:45:41

标签: android android-fragments android-activity

这是我使用按钮从片段中打开新活动的代码。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.hello_world, container, false);

    Button newActivity = (Button) v.findViewById(R.id.click);
    newActivity.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            goToActivity();
        }
    });
    return v;
}

public void goToActivity(){
    Intent intent = new Intent(getActivity(), HomeActivity.class);
    startActivity(intent);
    getActivity().finish();
}

是否可以在没有触发按钮的情况下从片段中打开新活动?因为我在打开新活动时遇到了很多错误并且直接关闭片段活动:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.hello_world, container, false);

    goToActivity();
    return v;
}

public void goToActivity(){
    Intent intent = new Intent(getActivity(), HomeActivity.class);
    startActivity(intent);
    getActivity().finish();
}

编辑: 添加错误日志

06-14 11:52:45.394: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41b682e0 that was originally added here
06-14 11:52:45.394: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41b682e0 that was originally added here
06-14 11:52:45.394: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.394: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.394: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.394: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.394: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.394: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.394: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.394: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.394: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.394: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.394: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.394: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.394: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.394: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.394: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.394: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.394: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.394: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.394: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.394: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.394: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.394: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.394: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.394: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.404: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41a37398 that was originally added here
06-14 11:52:45.404: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41a37398 that was originally added here
06-14 11:52:45.404: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.404: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.404: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.404: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.404: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.404: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.404: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.404: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.404: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.404: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.404: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.404: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.404: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.404: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.404: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.404: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.404: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.404: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.404: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.404: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.404: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.404: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.404: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.404: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.414: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bd4ed8 that was originally added here
06-14 11:52:45.414: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bd4ed8 that was originally added here
06-14 11:52:45.414: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.414: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.414: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.414: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.414: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.414: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.414: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.414: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.414: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.414: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.414: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.414: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.414: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.414: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.414: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.414: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.414: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.414: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.414: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.414: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.414: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.414: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.414: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.414: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.424: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bbadd0 that was originally added here
06-14 11:52:45.424: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41bbadd0 that was originally added here
06-14 11:52:45.424: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.424: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.424: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.424: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.424: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.424: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.424: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.424: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.424: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.424: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.424: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.424: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.424: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.424: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.424: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.424: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.424: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.424: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.424: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.424: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.424: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.424: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.424: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.424: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.434: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c2cf80 that was originally added here
06-14 11:52:45.434: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c2cf80 that was originally added here
06-14 11:52:45.434: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.434: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.434: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.434: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.434: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.434: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.434: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.434: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.434: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.434: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.434: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.434: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.434: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.434: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.434: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.434: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.434: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.434: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.434: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.434: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.434: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.434: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.434: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.434: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.444: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c00460 that was originally added here
06-14 11:52:45.444: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c00460 that was originally added here
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.444: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.444: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.444: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.444: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.444: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.444: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.444: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.444: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41cd3e60 that was originally added here
06-14 11:52:45.444: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41cd3e60 that was originally added here
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.444: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.444: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.444: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.444: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.444: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.444: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.444: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.444: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.444: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.444: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.464: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ce2fa8 that was originally added here
06-14 11:52:45.464: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41ce2fa8 that was originally added here
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.464: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.464: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.Looper.loop(Looper.java:137)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.ActivityThread.main(ActivityThread.java:4794)
06-14 11:52:45.464: E/WindowManager(3501):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 11:52:45.464: E/WindowManager(3501):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 11:52:45.464: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-14 11:52:45.464: E/WindowManager(3501):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-14 11:52:45.464: E/WindowManager(3501):  at dalvik.system.NativeStart.main(Native Method)
06-14 11:52:45.464: E/WindowManager(3501): Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c66798 that was originally added here
06-14 11:52:45.464: E/WindowManager(3501): android.view.WindowLeaked: Activity com.arbalest.example.NewActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c66798 that was originally added here
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
06-14 11:52:45.464: E/WindowManager(3501):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.Dialog.show(Dialog.java:277)
06-14 11:52:45.464: E/WindowManager(3501):  at com.arbalest.example.HomeFragment$LoadPanoramaJSON.onPreExecute(HomeFragment.java:95)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.AsyncTask.execute(AsyncTask.java:534)
06-14 11:52:45.464: E/WindowManager(3501):  at com.arbalest.example.HomeFragment.onCreateView(HomeFragment.java:75)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.BackStackRecord.run(BackStackRecord.java:635)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
06-14 11:52:45.464: E/WindowManager(3501):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 11:52:45.464: E/WindowManager(3501):  at android.os.Handler.dispatchMessage(Handler.java:92)

..还有更多..

2 个答案:

答案 0 :(得分:1)

首先,您无需以这种方式致电活动.finish()。 android Activity生命周期会自动为您处理 当您使用startActivity(intent)开始新活动时,之前的活动会自动调用其生命周期方法(最初为onPause(),然后为onStop()等)。

其次,活动显示片段。为什么在创建片段视图后,您是否希望片段启动不同的活动?我想您可能需要进一步了解FragmentActivity生命周期。


附加:
当您启动新活动时,之前的活动会添加到Backstack中。您可以将backstack视为应用中活动的历史记录。每次按下后退按钮,都会返回应用程序的活动历史记录。您可以通过在AndroidManifest.xml文件中为该特定活动设置android:noHistory=true"来停止将活动添加到Backstack。

<activity
            android:name="xxx.xxxxxxx.xxxxxxx.MyActivity"
            android:label="@string/app_name"
            android:noHistory="true"  
            android:configChanges="orientation"
            android:screenOrientation="portrait"/>

答案 1 :(得分:0)

尝试从onResume()中调用goToActivity()方法。