2017-02-21 44 views
-2

recyclerview我試圖將一個RecyclerView單個項目的ID傳遞給我的DetailsActivity.java,但運行到一個NullPointerException。nullPointerException while將數據傳遞給一個新類

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

顯然'DetailsActivity'沒有啓動任何意圖內臨時演員。你爲什麼認爲這是? –

+0

我對此很新,複製並粘貼我的代碼,然後根據我的需要進行調整。當我放置斷點時,我可以看到我的recyclerdapter中添加的數據,但它沒有達到我的具體活動 – MarkC

+0

沒問題。你有沒有學習足夠的java知道如何簡單地做'if(someObject!= null){}'? –

回答

1

我想你忘了把捆你正在創建的onClick方法

intent.putExtras(extras); 
+0

這已修復代碼,謝謝。 – MarkC