无法在RecyclerViewCategoryAdapter.setClickListener

时间:2017-01-20 10:34:33

标签: android android-recyclerview adapter

我在Adapter和RecyclerView中设置了方法:setClickListener(),当点击项目时,它将转换到另一个活动。

我的Recycler类:

public class CategoryCarActivity extends AppCompatActivity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener, ItemClickListener {

RecyclerViewCategoryAdapter recyclerViewAdapter;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewLayoutManager;
private LinearLayout linearLayoutScroll;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recycler_category_adapter);      // activity_sgv

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerViewLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(recyclerViewLayoutManager);
    // Loading tracks in Background Thread
    new LoadCategoryCars().execute();

    recyclerViewAdapter.setClickListener(this);
}
@Override
public void onClick(View view, int position) {
    Intent i = new Intent(getApplicationContext(), DetailListCarActivityDemo1.class);

    String album_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
    String price = ((TextView) view.findViewById(R.id.price)).getText().toString();

    Toast.makeText(getApplicationContext(), "Car Id: " + car_id  + ", Price: " + price, Toast.LENGTH_SHORT).show();

    i.putExtra("car_id", car_id);
    i.putExtra("price", price);

    Log.i("Car ID: ", car_id);
    Log.i("Price: ", price);

    startActivity(i);
}

// Initialize Toolbar the first time
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.map_icon_toolbar, menu);
    return true;
}

@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {
    Log.d(TAG, "onScroll firstVisibleItem:" + firstVisibleItem +
            " visibleItemCount:" + visibleItemCount +
            " totalItemCount:" + totalItemCount);
    // our handling
    if (!mHasRequestedMore) {
        int lastInScreen = firstVisibleItem + visibleItemCount;
        if (lastInScreen >= totalItemCount) {
            Log.d(TAG, "onScroll lastInScreen - so load more");
            mHasRequestedMore = true;
            // onLoadMoreItems();
        }
    }
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
}
/**
 * Background Async Task to Load all tracks under one album
 * */
class LoadCategoryCars extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    protected String doInBackground(String... args) {
        ...
    }    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CategoryCarActivity.this);
        pDialog.setMessage("Loading cars ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected void onPostExecute(String result) {

        super.onPostExecute(result);

        recyclerViewAdapter = new RecyclerViewCategoryAdapter(carsList, CategoryCarActivity.this);
        recyclerViewAdapter.notifyDataSetChanged();

        recyclerView.setAdapter(recyclerViewAdapter);
        // dismiss the dialog after getting all manufacturers
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
  } 
 }

我的适配器类:

public class RecyclerViewCategoryAdapter extends RecyclerView.Adapter<RecyclerViewCategoryAdapter.ViewHolder>  {
Context context;
ArrayList<HashMap<String, String>> getCarData;  // getDataAdapter
ImageLoader imageThumbLoader;
private ItemClickListener clickListener;

public RecyclerViewCategoryAdapter(ArrayList<HashMap<String, String>> getCarData, Context context){
    super();
    this.getCarData = getCarData;
    this.context = context;
}

@Override
public RecyclerViewCategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_car_info, parent, false);
    RecyclerViewCategoryAdapter.ViewHolder viewHolder = new RecyclerViewCategoryAdapter.ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerViewCategoryAdapter.ViewHolder viewholder, int position) {
    HashMap<String,String> map = getCarData.get(position);
    imageThumbLoader = ServerImageParseAdapter.getInstance(context).getImageLoader();        
    viewholder.price.setText(map.get(CategoryCarActivity.TAG_PRICE));
}
public void setClickListener(ItemClickListener itemClickListener) {
    this.clickListener = itemClickListener;
}

private boolean isPositionHeader(int position) {
    return position == 0;
}

class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public NetworkImageView imageThumb;
    public TextView price;

    public ViewHolder(View itemView) {

        super(itemView);

        titleName = (TextView) itemView.findViewById(R.id.titleName);
        imageThumb = (NetworkImageView) itemView.findViewById(R.id.imageThumb);
        price = (TextView) itemView.findViewById(R.id.price);

        itemView.setOnClickListener(this);
   }
    @Override
    public void onClick(View view) {
        if (clickListener != null) clickListener.onClick(view, getAdapterPosition());
    }
   }
 }

我已经调试并发现构建时出现问题。

Logcat消息:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.totoroads.android.app, PID: 3676
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.totoroads.android.app/com.totoroads.android.app.CategoryCarActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.totoroads.android.app.RecyclerViewCategoryAdapter.setClickListener(com.totoroads.android.app.ItemClickListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6119)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.totoroads.android.app.RecyclerViewCategoryAdapter.setClickListener(com.totoroads.android.app.ItemClickListener)' on a null object reference
                  at com.totoroads.android.app.CategoryCarActivity.onCreate(CategoryCarActivity.java:175)
                  at android.app.Activity.performCreate(Activity.java:6679)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 

如何解决问题?非常感谢!

2 个答案:

答案 0 :(得分:0)

您异步执行任务然后在onCreate中尝试访问尚未为null的recycleview适配器,因为在异步任务中执行任务,因此必须在onPostExecute中设置适配器,如下所示:

public class CategoryCarActivity extends AppCompatActivity implements AbsListView.OnScrollListener, AbsListView.OnItemClickListener, ItemClickListener {

RecyclerViewCategoryAdapter recyclerViewAdapter;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewLayoutManager;
private LinearLayout linearLayoutScroll;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recycler_category_adapter);      // activity_sgv

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerViewLayoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(recyclerViewLayoutManager);
    // Loading tracks in Background Thread
    new LoadCategoryCars().execute();


}
@Override
public void onClick(View view, int position) {
    Intent i = new Intent(getApplicationContext(), DetailListCarActivityDemo1.class);

    String album_id = ((TextView) view.findViewById(R.id.manufacturer_id)).getText().toString();
    String price = ((TextView) view.findViewById(R.id.price)).getText().toString();

    Toast.makeText(getApplicationContext(), "Car Id: " + car_id  + ", Price: " + price, Toast.LENGTH_SHORT).show();

    i.putExtra("car_id", car_id);
    i.putExtra("price", price);

    Log.i("Car ID: ", car_id);
    Log.i("Price: ", price);

    startActivity(i);
}

// Initialize Toolbar the first time
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.map_icon_toolbar, menu);
    return true;
}

@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {
    Log.d(TAG, "onScroll firstVisibleItem:" + firstVisibleItem +
            " visibleItemCount:" + visibleItemCount +
            " totalItemCount:" + totalItemCount);
    // our handling
    if (!mHasRequestedMore) {
        int lastInScreen = firstVisibleItem + visibleItemCount;
        if (lastInScreen >= totalItemCount) {
            Log.d(TAG, "onScroll lastInScreen - so load more");
            mHasRequestedMore = true;
            // onLoadMoreItems();
        }
    }
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    Toast.makeText(this, "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
}
/**
 * Background Async Task to Load all tracks under one album
 * */
class LoadCategoryCars extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    protected String doInBackground(String... args) {
        ...
    }    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CategoryCarActivity.this);
        pDialog.setMessage("Loading cars ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected void onPostExecute(String result) {

        super.onPostExecute(result);

        recyclerViewAdapter = new RecyclerViewCategoryAdapter(carsList, CategoryCarActivity.this);
        recyclerViewAdapter.notifyDataSetChanged();

        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerViewAdapter.setClickListener(this);
        // dismiss the dialog after getting all manufacturers
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
  } 
 }

答案 1 :(得分:0)

recyclerViewAdapter为空。你在呼叫recyclerViewAdapter.setClickListener(this); 在设置完成执行new LoadCategoryCars().execute();

的适配器之前

删除此行recyclerViewAdapter.setClickListener(this);