때때로이 오류는에 대한 명시적인 호출없이 발생할 수 있습니다 startActivity(...)
. 예를 들어, 여러분 중 일부는 Crashlytics에서 다음과 같은 스택 추적을 보았을 것입니다.
Fatal Exception: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1597)
at android.app.ContextImpl.startActivity(ContextImpl.java:1584)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:337)
at android.text.style.URLSpan.onClick(URLSpan.java:62)
at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
at android.widget.TextView.onTouchEvent(TextView.java:9522)
at android.view.View.dispatchTouchEvent(View.java:8968)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.widget.AbsListView.dispatchTouchEvent(AbsListView.java:5303)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2559)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1767)
at android.app.Activity.dispatchTouchEvent(Activity.java:2866)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2520)
at android.view.View.dispatchPointerEvent(View.java:9173)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4706)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4544)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4201)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4258)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4121)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4087)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4095)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4068)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6564)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6454)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6425)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6654)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
at android.os.MessageQueue.next(MessageQueue.java:143)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
추적에는 프레임 워크 코드 만 포함되어 있기 때문에 무엇이 잘못되었는지 궁금 할 수 있습니다. 자, 이것이 어떻게 일어날 수 있는지에 대한 예입니다. 우리가 조각에 있다고 가정 해 봅시다.
Activity activity = getActivity();
Context activityContext = activity;
Context appContext = activityContext.getApplicationContext();
LayoutInflater inflater = LayoutInflater.from(appContext); // whoops!
View view = inflater.inflate(R.layout.some_layout, parent, false);
TextView tvWithLinks = (TextView) view.findViewById(R.id.tv_with_links);
tvWithLinks.setMovementMethod(LinkMovementMethod.getInstance()); // whoops!!
이제 사용자가 해당 텍스트보기를 클릭하면 위의 스택 추적과 함께 앱이 중단됩니다. 때문이다 레이아웃 인플레이터가 받는 참조가 응용 프로그램 컨텍스트를 , 그래서 그러므로 당신의 텍스트보기는 애플리케이션 컨텍스트가 있습니다. 해당 텍스트보기를 클릭하면 암시 적으로 호출 appContext.startActivity(...)
됩니다.
마지막 참고 사항 : Android 4, 5, 6 및 7 장치에서 테스트했습니다. 4, 5 및 6에만 영향을 미칩니다. Android 7 기기는 전화하는 데 아무런 문제가 없습니다 appContext.startActivity(...)
.
나는 이것이 다른 누군가를 돕기를 바랍니다!