-2

RecyclerView単一項目のIDをDetailsActivity.javaに渡していますが、NullPointerExceptionが発生しています。nullPointerException新しいクラスにデータを渡しているときにrecyclerview

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)' 
+0

を作成している意図内のバンドルを置くことを忘れてしまったと思いますエクストラ。なぜあなたはそれがだと思いますか? –

+0

私はこれにかなり新しいので、自分のコードをコピーして貼り付けて、それを自分のニーズに合わせました。ブレークポイントを置くと、私のrecyclerdapterに追加されたデータを見ることができますが、それは私のdetailsアクティビティには届きません – MarkC

+0

これは問題ありません。単純に 'if(someObject!= null){}'を行う方法を知るには十分なJavaを学んだことがありますか? –

答えて

1

私はあなたがどうやら `DetailsActivity`はいずれにも開始されませんでしたのonClickメソッドに

intent.putExtras(extras); 
+0

これはコードを修正しました。ありがとうございました。 – MarkC

関連する問題