ListView中的OnItemClick在NullObject Regerence上抛出NullPointer异常

时间:2016-10-20 05:04:34

标签: java android listview android-intent

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)

我在ListView中使用BaseAdapter显示一些数据,但是当我使用onItemclickListener显示完整数组列表的内容时,它会在{Null对象引用上抛出NullPointerException。 在这里我使用Arraylist<Model>和List来设置数据,我使用Intent传递listView位置,我通过intent获得该位置,并将该位置分配给ArrayList。我在下面发布我的代码指导我做错了什么< / p>

我在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();
}}

我需要在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);
}}

2 个答案:

答案 0 :(得分:0)

您需要使用intent将数据传递给AppointmentActivity。

您可以从reservationLists获取相应的数据,而不是传递位置,并使用意图传递该数据。现在,在AppointmentActivity中获取该数据并直接使用它。

在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();
}

现在在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());
//....

}

此外,如 MSS 所述,您需要在初始化活动中的任何视图之前为Activity设置视图。

答案 1 :(得分: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.