2016-10-20 3 views
-1

OnItemClick in Listview wirft Nullpointer Exception auf 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)

Ich angezeigte einige Daten in ListViewBaseAdapter verwenden, das funktioniert gut, aber wenn ich onItemclickListener den Inhalt voll arrayoflist zu zeigen wirft NullPointerException auf Null Objektreferenz. hier ich Arraylist<Model> und Liste bin mit Daten zu setzen und ich gehe die Listview-Position Intent mit und ich diese Position durch Absicht zu bekommen und ich zuweisen diese Position meines Code unter mir zu Arraylist .Ich Post Was ich falsch

täte

Klasse, wo ich den Wert auf Array List und Intent in 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(); 
}} 

Klasse, wo ich in Textview

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

Wo haben Sie initializedLists in AppointmentActivity? – Raghavendra

+0

Ihre 'reservationLists' ist null in Ihrer' TerminAktivität'. Initialisiere 'reservationLists' – SripadRaj

+0

Ich habe Reservierungslisten auf '' terminationActivity '' initialisiert aber jetzt 'verursacht durch: java.lang.IndexOutOfBoundsException: Ungültiger Index 0, Größe ist 0 bei java.util.ArrayList.throwIndexOutOfBoundsException (ArrayList.java:255) bei java.util .ArrayList.get (ArrayList.java308) unter com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.callDataList (AppointmentActivity.java:45) unter com.targetsoftsystem.sonyproject.activity.activity.AppointmentActivity.onCreate (AppointmentActivity.java : 37) – Rahul

Antwort

0

Sie vollständige Daten der Liste angezeigt werden müssen, die Daten zu Appo passieren intmentActivity mit Absicht.

Anstatt die Position zu übergeben, können Sie die entsprechenden Daten von reservationLists erhalten und diese mit Absicht übergeben. Jetzt, in AppointmentActivity erhalten Sie diese Daten und verwenden Sie sie direkt.

In ReservationActivity der 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(); 
} 

Jetzt in 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()); 
//.... 

}

wie MSS erwähnt Außerdem müssen Sie für Aktivität eine Ansicht setzen, bevor sie eine Ansicht bei der Initialisierung Aktivität.

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. 
Verwandte Themen