android - 使用 AsyncTask 时上下文为空

android - context null while using AsyncTask

在我下面的代码中,上下文为 null。不知道怎么解决....请帮帮我,

我正在使用 RecyclerView,在 onBindViewHolderonClick 上下文变为 NULL。可能是因为我得到空指针异常。还有一个关键点在 mainActivity

mAdapter = new CardAdapter(mContext);  
mRecyclerView.setAdapter(mAdapter);

当我在 AsyncTask 中使用上述代码时出现错误....如果我不使用 AsyncTask 我不会收到任何错误,

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

List<NatureItem> mItems;
public Context context;
NatureItem nature = new NatureItem();

public CardAdapter(Context context) {
    super();
    this.context = context;
    mItems = new ArrayList<NatureItem>();

    nature = new NatureItem();
    nature.setName("Aurora Borealis");
    nature.setThumbnail(R.drawable.aurora_borealis);
    mItems.add(nature);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.recycler_view_card_item, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder viewHolder, int i) {

    nature = mItems.get(i);
    viewHolder.textData.setText(nature.getName());
    viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());

    if(context!=null)

    viewHolder.commentClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, Recorder.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });

    if(context!=null)

    viewHolder.profileCLick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, Profile_Activity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    });

}

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

class ViewHolder extends RecyclerView.ViewHolder{
    public ImageView imgThumbnail;
    public TextView textData;
    public ImageButton profileCLick;
    public ImageButton commentClick;

    public ViewHolder(View itemView) {
        super(itemView);
        imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
        textData = (TextView)itemView.findViewById(R.id.tv_nature);
        profileCLick = (ImageButton) itemView.findViewById(R.id.imageButton11);
        commentClick = (ImageButton) itemView.findViewById(R.id.imageButton12);

    }
}
}

我在 MainActivity 中的 AsyncTask 代码

    Context mContext;
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
      mRecyclerView.setHasFixedSize(true);

      mLayoutManager = new LinearLayoutManager(this);
      mRecyclerView.setLayoutManager(mLayoutManager);
      registerForContextMenu(mRecyclerView);

      new RemoteDataTask().execute();

    }

    // RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        mAdapter = new CardAdapter(mContext);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        mRecyclerView.setAdapter(mAdapter);
        mProgressDialog.dismiss();

    }
}

我收到错误:

java.lang.NullPointerException
        at android.content.ComponentName.<init>(ComponentName.java:75)
        at android.content.Intent.<init>(Intent.java:3697)
        at com.example.intellisoft.a2a.CardAdapter.onClick(CardAdapter.java:189)
        at android.view.View.performClick(View.java:4293)
        at android.view.View$PerformClick.run(View.java:17535)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:153)
        at android.app.ActivityThread.main(ActivityThread.java:5299)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        at dalvik.system.NativeStart.main(Native Method)

在 onClick() 方法中,您是否尝试过从 'v' - 视图对象获取上下文。 喜欢,

Context context = v.getContext();

然后,检查您是否可以使用此 'context' 对象启动 activity。

尝试在您的 onCreate() 方法中添加以下行:

mContext = this;

你得到了 null,因为你传递了 null 上下文。