将数据传递给新类时,nullPointerException为recyclelerview

时间:2017-02-21 14:41:09

标签: android nullpointerexception android-recyclerview bundle

我正在尝试将RecyclerView单项的id传递给我的DetailsActivity.java,但是遇到了NullPointerException。

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


static List<DatabaseModel> dbList;
static Context context;
RecyclerAdapter(Context context, List<DatabaseModel> dbList){
    this.dbList = new ArrayList<DatabaseModel>();
    this.context = context;
    this.dbList = dbList;

}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.item_row, null);

    // create ViewHolder

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {

    holder.address_h.setText(dbList.get(position).getAddress());
    holder.name_h.setText(dbList.get(position).getName());
    holder.date_h.setText("Due:" + dbList.get(position).getSpare1());
    holder.balance_h.setText("Owes £" + dbList.get(position).getBalance());
    holder.amount_h.setText("Regular Price £" + dbList.get(position).getAmount());


    holder.address_h.setSelected(holder.address_h.isSelected());
    holder.name_h.setSelected(holder.name_h.isSelected());
    holder.date_h.setSelected(holder.date_h.isSelected());
    holder.balance_h.setSelected(holder.balance_h.isSelected());
    holder.amount_h.setSelected(holder.amount_h.isSelected());
}

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

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView amount_h, address_h, balance_h, name_h, date_h;

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


        address_h = (TextView) itemLayoutView.findViewById(R.id.rvaddress);
        balance_h = (TextView)itemLayoutView.findViewById(R.id.rvphone);
        name_h = (TextView)itemLayoutView.findViewById(R.id.rvname_h);
        date_h = (TextView)itemLayoutView.findViewById(R.id.rvdate_h);
        amount_h = (TextView)itemLayoutView.findViewById(R.id.rv_regular_price);

        itemLayoutView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int adapterPosition_id=getAdapterPosition();
        int position_id=dbList.get(adapterPosition_id).get_id();

        Bundle extras = new Bundle();
        extras.putInt("position", position_id);
        Intent intent = new Intent(context,DetailsActivity.class);

        context.startActivity(intent);
    }
}
}

public class DetailsActivity extends AppCompatActivity {


    DatabaseHelper helper;
    List<DatabaseModel> dbList, dbPaidList;
    int position, dayPeriod;
    TextView tvid, tvname, tvaddress, tvbalance, posOrNegBalance;
    Button btnDone, btnPaid;
    Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_details);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        position = bundle.getInt("position"); // **This is where my code breaks down**

        helper = new DatabaseHelper(this);

我的错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.co.freerolhq.windowscashbook/uk.co.freerolhq.windowscashbook.DetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
                      at android.app.ActivityThread.access$800(ActivityThread.java:178)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:194)
                      at android.app.ActivityThread.main(ActivityThread.java:5637)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
                      at uk.co.freerolhq.windowscashbook.DetailsActivity.onCreate(DetailsActivity.java:49)
                      at android.app.Activity.performCreate(Activity.java:6092)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                      at android.os.Handler.dispatchMessage(Handler.java:111) 
                      at android.os.Looper.loop(Looper.java:194) 
                      at android.app.ActivityThread.main(ActivityThread.java:5637) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)'

1 个答案:

答案 0 :(得分:1)

我认为您忘记将捆绑包放在您在onClick方法中创建的意图

intent.putExtras(extras);