为什么无法在Android中自定义寻呼机适配器内打开活动?

时间:2016-04-10 11:06:56

标签: android android-pageradapter

我是开发人员的Android应用。在我的应用程序中,我使用寻呼机适配器创建自定义图像寻呼机。我想要的是当我点击寻呼机的当前图像时我想开始图像活动。所以我尝试在适配器内启动活动。我的寻呼机工作正常,除了开始活动。但是当我开始活动时,它会抛出错误。我该如何解决?

这是我的寻呼机适配器以及我如何在其中启动活动:

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

这是我开始点击图片时的logcat:

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)

2 个答案:

答案 0 :(得分:1)

添加 - FLAG_ACTIVITY_NEW_TASK标记到您的意图:

myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     

或者你应该尝试添加标志:

myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

答案 1 :(得分:0)

显然,

mContext不是Activity。它应该是,就像你应该mLayoutInflater通过getLayoutInflater()调用Activity一样,所以你的主题得到尊重。 而且,由于您没有实现任何正常的View构造函数,很明显您是创建ItemImagePager实例的人,而不是执行它的框架(例如,通过膨胀一些布局)。

所以,你的选择是:

  1. 更改创建ItemImagePager的代码,以传递Activity作为第一个参数(理想情况下,修复LayoutManager位),或

  2. 阅读错误消息并完全按照其要求执行操作:将FLAG_ACTIVITY_NEW_TASK添加到Intent