I am developer an Android app. In my app, I am creating custom image pager using pager adapter. What I want is I want to start image activity when I click on the current image of pager. So I tried to start the activity inside adapter. My pager is working fine except starting activity. But when I start activity, it is throwing error. How can I fix it?
This is my pager adapter and how I start activity inside it:
public class ItemImagePager extends PagerAdapter{
Context mContext;
LayoutInflater mLayoutInflater;
ArrayList<String> mResources;
public ItemImagePager(Context context,ArrayList<String> urls) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mResources = urls;
}
@Override
public int getCount() {
return mResources.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View itemView = mLayoutInflater.inflate(R.layout.item_image_pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.image_view_item_image_pager);
Picasso.with(mContext).load(mResources.get(position)).into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(mContext, ViewImageActivity.class);
i.putExtra("url",mResources.get(position));
mContext.startActivity(i);
}
});
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
This is the logcat when I start activity clicking the image:
04-10 11:05:37.698 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion W/EGL_genymotion: eglSurfaceAttrib not implemented
04-10 11:05:37.742 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion W/EGL_genymotion: eglSurfaceAttrib not implemented
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion D/AndroidRuntime: Shutting down VM
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa6184908)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: FATAL EXCEPTION: main
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: 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-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ContextImpl.startActivity(ContextImpl.java:952)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ContextImpl.startActivity(ContextImpl.java:939)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.blog.waiyanhein.mmfashion.model.ItemImagePager$1.onClick(ItemImagePager.java:54)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.View.performClick(View.java:4204)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17355)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:725)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 11:05:38.938 28934-28934/com.blog.waiyanhein.mmfashion.mmfashion E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
add - FLAG_ACTIVITY_NEW_TASK flag to your intent:
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Or you should try by adding flags:
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);