从RecyclerView错误中调用活动

时间:2015-05-07 08:27:49

标签: android android-recyclerview

我尝试在RecyclerView附加 onitem click listner 的帮助下调用来自onBindViewHolder的活动。 当我尝试启动活动时,会显示 null pointer exception 并发生错误。

来自main activity我正在通过datasetcontext并借助该背景我正在尝试开展一项活动

以下是我的代码

public class mycard extends RecyclerView.Adapter<mycard.ViewHolder> {
static ArrayList<String> mDataset = new ArrayList<>();
static Integer[] image;
static Context c;

public mycard(ArrayList<String> main,Integer[] images,Context context) {
    mDataset = main;
    image=images;
    c=context;

}
public static class ViewHolder extends RecyclerView.ViewHolder  {
    // each data item is just a string in this case
    public TextView mTextView;
    public ImageView img;

    public ViewHolder(View v) {
        super(v);

        mTextView = (TextView) v.findViewById(R.id.info_text);
        img=(ImageView)v.findViewById(R.id.cardimage);
    }


}


@Override
public mycard.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.cardview, viewGroup, false);
    // set the view's size, margins, paddings and layout parameters

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder,  final int i) {
    viewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int positon = i;
            Intent intent = new Intent(c,ContactUs.class);
            c.startActivity(intent);
        }
    });
    viewHolder.mTextView.setText(mDataset.get(i));
    viewHolder.img.setImageResource(image[i]);
}

@Override
public int getItemCount() {
    return mDataset.size();
}

}

Follwing ERROR I GATING

  

05-07 13:50:58.604 9767-9767 / aptlegion.vivatoyata E / AndroidRuntime:   致命异议:主要       过程:aptlegion.vivatoyata,PID:9767       显示java.lang.NullPointerException               在android.content.ComponentName。(ComponentName.java:77)               在android.content.Intent。(Intent.java:3826)               at aptlegion.vivatoyata.mycard $ 1.onClick(mycard.java:60)               在android.view.View.performClick(View.java:4569)               在android.view.View $ PerformClick.run(View.java:18570)               在android.os.Handler.handleCallback(Handler.java:743)               在android.os.Handler.dispatchMessage(Handler.java:99)               在android.os.Looper.loop(Looper.java:136)               在android.app.ActivityThread.main(ActivityThread.java:5196)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)               在dalvik.system.NativeStart.main(本地方法)

AND IN MAIN ACTIVTY

 RecyclerView mRecyclerView = (RecyclerView)        findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new mycard(main ,imageId,context);
    mRecyclerView.setAdapter(mAdapter);

请帮助我解决问题

0 个答案:

没有答案