
我打算在我的项目中加入一个CardVIEw.我已经在我的项目中包含了RecyclerVIEw和卡片视图.问题是,我想为每张卡调用不同的活动.我已经为每张卡实现了不同的意图.但它需要我初始化数据.这是我的原始代码:
public class RecyclerAdapter extends RecyclerVIEw.Adapter<RecyclerAdapter.VIEwHolder> {private Context context;private String[] Titles = {"Add new Research", "VIEw Your Research"};private String[] details = {"Add your research files here", "VIEw all of your posted research"};private int[] images = { R.drawable.add, R.drawable.vIEw};class VIEwHolder extends RecyclerVIEw.VIEwHolder{ public int currentItem; public ImageVIEw itemImage; public TextVIEw itemTitle; public TextVIEw itemDetail; public VIEwHolder(VIEw itemVIEw) { super(itemVIEw); itemImage = (ImageVIEw)itemVIEw.findVIEwByID(R.ID.item_image); itemTitle = (TextVIEw)itemVIEw.findVIEwByID(R.ID.item_Title); itemDetail = (TextVIEw)itemVIEw.findVIEwByID(R.ID.item_detail); itemVIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { int position = getAdapterposition(); Intent intent = new Intent(context, Choose.class); if(position==0){ intent = new Intent(context, Addfiles.class); }else if(position==1){ intent = new Intent(context, VIEwfiles.class); } context.startActivity(intent); } }); }}当我点击卡片视图时,它表示我的程序没有响应.
即使我将意图初始化为
Intent intent = null; if(position==0){ intent = new Intent(context, Addfiles.class); }else if(position==1){ intent = new Intent(context, VIEwfiles.class); } context.startActivity(intent);还有错误,我该怎么办?或者有更好的方法来做到这一点.
这是我的logcat错误.
E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.user.mcormpelo, PID: 3645 @R_404_4126@.NullPointerException: Attempt to invoke virtual method '@R_404_4126@.String androID.content.Context.getPackagename()' on a null object reference at androID.content.Componentname.<init>(Componentname.java:128) at androID.content.Intent.<init>(Intent.java:4449) at com.example.user.mcormpelo.RecyclerAdapter$VIEwHolder.onClick(RecyclerAdapter.java:46) at androID.vIEw.VIEw.performClick(VIEw.java:5198) at androID.vIEw.VIEw$PerformClick.run(VIEw.java:21147) at androID.os.Handler.handleCallback(Handler.java:739) at androID.os.Handler.dispatchMessage(Handler.java:95) at androID.os.Looper.loop(Looper.java:148) at androID.app.ActivityThread.main(ActivityThread.java:5417) at @R_404_4126@.reflect.Method.invoke(Native Method) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:616)解决方法:
您没有初始化上下文
public class RecyclerAdapter extends RecyclerVIEw.Adapter<RecyclerAdapter.VIEwHolder> { private Context context; public RecyclerAdapter(Context context) { this.context = context; }从调用者活动传递Activity引用.
样品用法
RecyclerAdapter rcAdapter = new RecyclerAdapter(MainActivity.this); 总结 以上是内存溢出为你收集整理的android – 如何在recyclerview中包含不同的意图全部内容,希望文章能够帮你解决android – 如何在recyclerview中包含不同的意图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)