0

I got this error while display data from adapter set in service.

This error specific come in below android M versions.

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]

I done in adapter this also:

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"));

I set adapter from service:

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

In adapter I get context in constructor and do above share code. I may cause because of context problem but I did not know what actual is.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Krishna Kachhela
  • 773
  • 1
  • 7
  • 24
  • Possible duplicate of [Calling startActivity() from outside of an Activity context](https://stackoverflow.com/questions/3918517/calling-startactivity-from-outside-of-an-activity-context) – AskNilesh Apr 11 '18 at 11:12
  • but solution not worked because adapter us set in service not in activity ana my service start when my app close and other app open so it cant fing getApplictionContext(); – Krishna Kachhela Apr 11 '18 at 11:13

0 Answers0