从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。这真的是你想要的吗?

时间:2018-04-11 11:11:21

标签: android android-intent adapter android-5.0-lollipop android-context

我在使用适配器设置显示数据时出现此错误。

此错误具体见于Android M版本。

04-11 04:24:21.727: E/AndroidRuntime(25540): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.app.ContextImpl.startActivity(ContextImpl.java:1296)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.app.ContextImpl.startActivity(ContextImpl.java:1283)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.content.ContextWrapper.startActivity(ContextWrapper.java:329)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at com.example.adapter.ImageAndVideoDisplayAdapter$1.onClick(ImageAndVideoDisplayAdapter.java:93)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.view.View.performClick(View.java:4762)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.view.View$PerformClick.run(View.java:19774)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.os.Handler.handleCallback(Handler.java:739)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.os.Looper.loop(Looper.java:135)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at android.app.ActivityThread.main(ActivityThread.java:5296)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at java.lang.reflect.Method.invoke(Native Method)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at java.lang.reflect.Method.invoke(Method.java:372)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-11 04:24:21.727: E/AndroidRuntime(25540):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-11 04:24:22.923: E/Sensors(870): [Sensor_HAL] : accel [0.425032] [2.300706] [9.610083]
04-11 04:24:23.457: E/Sensors(870):  Light adc=114, lux_tmp = 108.300003
04-11 04:24:23.457: E/Sensors(870): report event: Light adc=114, lux_float = 114.949997
04-11 04:24:24.260: E/Sensors(870):  Light adc=143, lux_tmp = 135.850006
04-11 04:24:24.261: E/Sensors(870): report event: Light adc=143, lux_float = 135.850006
04-11 04:24:24.846: E/Sensors(870): [Sensor_HAL] : accel [1.248637] [0.002273] [9.112089]
04-11 04:24:26.769: E/Sensors(870): [Sensor_HAL] : accel [1.306098] [-0.074342] [9.399393]
04-11 04:24:28.693: E/Sensors(870): [Sensor_HAL] : accel [1.516788] [-0.265878] [9.207857]
04-11 04:24:30.591: E/Sensors(870):  Light adc=200, lux_tmp = 190.000000
04-11 04:24:30.591: E/Sensors(870): report event: Light adc=200, lux_float = 190.000000
04-11 04:24:30.615: E/Sensors(870): [Sensor_HAL] : accel [1.076254] [-0.917101] [9.610083]

我也在适配器中完成了这个:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, modelStatus.filePath);
sharingIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
// sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(Intent.createChooser(sharingIntent, "Share image using"));

我从服务设置适配器:

adapter = new ImageAndVideoDisplayAdapter(getApplicationContext(), 5, list);

在适配器中,我在构造函数中获取上下文并执行上面的共享代码。我可能因为上下文问题而导致但我不知道实际是什么。

0 个答案:

没有答案