2016-10-20 2 views
-1

OnItemClick는 NullObject Regerence에 NullPointer 예외가 발생합니다

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)

나는 널에 NullPointerException을 예외 전체 arrayoflist의 내용을 표시하기 위해 잘 작동하지만 onItemclickListener를 사용할 때 BaseAdapter를 사용 ListView에서 일부 데이터를 표시하고

개체 참조. 여기 나는 데이터를 설정하는 Arraylist<Model> 및 목록을 사용하고 난 의도를 사용하여 목록보기 위치를 통과하고 내가 의도을 통해 그 위치를 얻을 내가 .I 내 코드는 아래에 내가 뭐하는 거지 무슨 나를 인도 게시 위해 ArrayList 그 위치를 지정

내가 onItemClickListener에서 나는 텍스트 뷰에서 목록의 전체 데이터를 표시해야

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입니다. '예약 목록 초기화 ' – 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) at 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. 
관련 문제