2016-10-20 6 views
-1

OnItemClickがNullObject Regerence

E/AndroidRuntime: FATAL EXCEPTION: main 
 
Process: com.targetsoftsystem.sonyproject, PID: 14326 
 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.targetsoftsystem.sonyproject/com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference 
 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) 
 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
 
at android.os.Handler.dispatchMessage(Handler.java:102) 
 
at android.os.Looper.loop(Looper.java:148) 
 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
 
at java.lang.reflect.Method.invoke(Native Method) 
 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference 
 
at com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.callDataList(AppointmentActivity.java:45) 
 
at com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.onCreate(AppointmentActivity.java:37) 
 
at android.app.Activity.performCreate(Activity.java:6259)

上にNullPointer例外がスローされます私は正常に動作しますが、私は完全なarrayoflistのコンテンツを表示するためにonItemclickListener使用する場合、それはヌルにNullPointerExceptionをスローBaseAdapterを使用してListViewでいくつかのデータを表示していますオブジェクト参照。 ここで私はArraylist<Model>を使用しています。データを設定するにはListを使用していますが、Intentを使用してlistViewの位置を渡し、その位置をIntentに渡してArrayListにその位置を割り当てます。私はonItemClickListenerに、私はTextViewの中で、リストの完全なデータを表示する必要が

public class ReservationActivity extends BaseActivity<DBAccess> implements VolleyTasksListener,AdapterView.OnItemClickListener{ 

private ListView lstReserve; 
private ArrayList<ReservationList> reservationLists; 

@Override 
protected void onCreate(Bundle arg0) { 
    super.onCreate(arg0); 
    setContentView(R.layout.activity_reservation); 

    findViews(); 
} 

private void findViews() { 
    lstReserve = (ListView)findViewById(R.id.lst_reservation); 
    lstReserve.setOnItemClickListener(this); 

    reservationLists = new ArrayList<ReservationList>(); 
    ReservationList reservationList = new ReservationList(); 
    reservationList.setName("Rahul"); 
    reservationList.setCCID("q123"); 
    reservationList.setServiceType("Repair"); 
    reservationList.setLocation("T Nagar"); 

    reservationList.setCallFrom("Delear"); 
    reservationList.setAppointmentAge("4"); 
    reservationList.setAppointmentDate("17-04-1992"); 
    reservationList.setDelearName("Sony Anna Nagar"); 
    reservationList.setDelearMobile("995222145"); 
    reservationList.setCustomerAddress("41/27 2nd Circular Road, JawaharNagar,Chennai-600082"); 

    ReservationList reservationList1 = new ReservationList(); 
    reservationList1.setName("Tamil"); 
    reservationList1.setCCID("q987"); 
    reservationList1.setServiceType("Service"); 
    reservationList1.setLocation("KK Nagar"); 

    ReservationList reservationList2 = new ReservationList(); 
    reservationList2.setName("Selvi"); 
    reservationList2.setCCID("W123"); 
    reservationList2.setServiceType("Service"); 
    reservationList2.setLocation("KK Nagar"); 

    ReservationList reservationList3 = new ReservationList(); 
    reservationList3.setName("Ravi"); 
    reservationList3.setCCID("W456"); 
    reservationList3.setServiceType("Instal"); 
    reservationList3.setLocation("Anna Nagar"); 

    reservationLists.add(reservationList); 
    reservationLists.add(reservationList1); 
    reservationLists.add(reservationList2); 
    reservationLists.add(reservationList3); 

    ReservationList reservationList4 = new ReservationList(); 
    reservationList4.setName("Rahul"); 
    reservationList4.setCCID("q123"); 
    reservationList4.setServiceType("Repair"); 
    reservationList4.setLocation("T Nagar"); 

    ReservationList reservationList5 = new ReservationList(); 
    reservationList5.setName("Tamil"); 
    reservationList5.setCCID("q987"); 
    reservationList5.setServiceType("Service"); 
    reservationList5.setLocation("KK Nagar"); 

    ReservationList reservationList6 = new ReservationList(); 
    reservationList6.setName("Selvi"); 
    reservationList6.setCCID("W123"); 
    reservationList6.setServiceType("Service"); 
    reservationList6.setLocation("KK Nagar"); 

    ReservationList reservationList7 = new ReservationList(); 
    reservationList7.setName("Ravi"); 
    reservationList7.setCCID("W456"); 
    reservationList7.setServiceType("Instal"); 
    reservationList7.setLocation("Anna Nagar"); 

    reservationLists.add(reservationList); 
    reservationLists.add(reservationList1); 
    reservationLists.add(reservationList2); 
    reservationLists.add(reservationList3); 
    reservationLists.add(reservationList4); 
    reservationLists.add(reservationList5); 
    reservationLists.add(reservationList6); 
    reservationLists.add(reservationList7); 

    ReservationAdapter reservationAdapter = new ReservationAdapter(ReservationActivity.this,reservationLists); 
    lstReserve.setAdapter(reservationAdapter); 
} 

@Override 
public void handleError(Exception error) { 

} 

@Override 
public void handleResult(String method_name, JSONObject response) { 

} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Intent intent = new Intent(this,AppointmentActivity.class); 
    intent.putExtra("Key_POSITION",position); 
    startActivity(intent); 
    finish(); 
}} 

クラスを配列リストと意図に値を設定するクラスは

public class AppointmentActivity extends BaseActivity<DBAccess> implements VolleyTasksListener { 

private TextView txtCallFrom; 
private TextView txtAppointmentAge; 
private TextView txtAppointmentDate; 
private TextView txtAddress; 
private TextView txtCustomerName; 
private TextView txtLocation; 
private TextView txtDelearName; 
private TextView txtDelearMobile; 
private int position; 
private ArrayList<ReservationList> reservationLists; 

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

    findViews(); 
    callDataList(); 
} 

private void callDataList() { 

    Intent intent = getIntent(); 
    position = intent.getExtras().getInt("Key_POSITION"); 

    txtCallFrom.setText("Call From : "+reservationLists.get(position).getCallFrom()); 
    txtAppointmentAge.setText("Call From : "+reservationLists.get(position).getAppointmentAge()); 
    txtAppointmentDate.setText("Call From : "+reservationLists.get(position).getAppointmentDate()); 
    txtAddress.setText("Call From : "+reservationLists.get(position).getCustomerAddress()); 
    txtCustomerName.setText("Call From : "+reservationLists.get(position).getName()); 
    txtLocation.setText("Call From : "+reservationLists.get(position).getLocation()); 
    txtDelearName.setText("Call From : "+reservationLists.get(position).getDelearName()); 
    txtDelearMobile.setText("Call From : "+reservationLists.get(position).getDelearMobile()); 
} 

private void findViews() { 
    txtCallFrom = (TextView)findViewById(R.id.txt_call_from); 
    txtAppointmentAge = (TextView)findViewById(R.id.txt_app_age); 
    txtAppointmentDate= (TextView)findViewById(R.id.txt_app_date); 
    txtAddress= (TextView)findViewById(R.id.txt_address); 
    txtCustomerName= (TextView)findViewById(R.id.txt_customer_name); 
    txtLocation= (TextView)findViewById(R.id.txt_location); 
    txtDelearName= (TextView)findViewById(R.id.txt_delear_name); 
    txtDelearMobile= (TextView)findViewById(R.id.txt_delear_mobile); 
}} 
+0

AppointmentActivityで予約リストを初期化していますか? – Raghavendra

+0

'あなたの' AppointmentActivity'の 'reservationLists'はnullです。 'reservationLists'を初期化します – SripadRaj

+0

私はappointmentActivityに予約リストを初期化しましたが、現在 '原因:java.lang.IndexOutOfBoundsException:無効なインデックス0、サイズは0です。java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)at java.util .ArrayList.get(ArrayList.java:308)at com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.callDataList(AppointmentActivity.java:45)com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.onCreate(AppointmentActivity.java) :37) ' – Rahul

答えて

0

あなたはAppoにデータを渡す必要がありますインテントを使ったintmentActivity。

位置を渡す代わりに、対応するデータをreservationListsから取得し、それをインテントを使用して渡すことができます。今、AppointmentActivityでそのデータを取得し、それを直接使用します。 MSSで述べたように

またAppointmentActivity

private void callDataList() { 

Intent intent = getIntent(); 
ReservationList reservationList = getIntent().getSerializableExtra("DATA"); 

txtCallFrom.setText("Call From : "+reservationList.getCallFrom()); 
txtAppointmentAge.setText("Call From : "+reservationList.getAppointmentAge()); 
txtAppointmentDate.setText("Call From : "+reservationList.getAppointmentDate()); 
//.... 

}で今ReservationActivityのonItemClick

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
Intent intent = new Intent(this,AppointmentActivity.class); 
intent.putExtra("DATA",reservationLists.get(position)); 
startActivity(intent); 
finish(); 
} 

は、あなたは内の任意のビューを初期化する前に、活動のためのビューを設定する必要がありますアクティビティ。

0
**findViews();** in AppointmentActivity.java cause the problem 

You are starting an activity(appointmentactivity) from ReservationActivity, but findViews() resides in ReservationActivity. 

When new activity appeared, You can not touch UI of previous activity and You can not get value from previous activity with out use of static/bundle/serialization. 
関連する問題