我正在编写一个程序,我需要在列表视图的每一行中使用按钮,但是每次我得到空指针异常,为什么?
日志说:
09-06 14:12:11.467: E/AndroidRuntime(530): FATAL EXCEPTION: main
09-06 14:12:11.467: E/AndroidRuntime(530): java.lang.NullPointerException
09-06 14:12:11.467: E/AndroidRuntime(530): at com.example.appointment.RescheduleActivity$ImageAdapter.getView(RescheduleActivity.java:162)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.AbsListView.obtainView(AbsListView.java:1970)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.ListView.measureHeightOfChildren(ListView.java:1228)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.ListView.onMeasure(ListView.java:1139)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.View.measure(View.java:10828)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:956)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.LinearLayout.onMeasure(LinearLayout.java:521)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.View.measure(View.java:10828)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1284)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:456)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.LinearLayout.measureVertical(LinearLayout.java:613)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.TableLayout.measureVertical(TableLayout.java:468)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.TableLayout.onMeasure(TableLayout.java:431)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.View.measure(View.java:10828)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.View.measure(View.java:10828)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.View.measure(View.java:10828)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
09-06 14:12:11.467: E/AndroidRuntime(530): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.View.measure(View.java:10828)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.ViewRoot.performTraversals(ViewRoot.java:909)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.os.Looper.loop(Looper.java:132)
09-06 14:12:11.467: E/AndroidRuntime(530): at android.app.ActivityThread.main(ActivityThread.java:4025)
09-06 14:12:11.467: E/AndroidRuntime(530): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 14:12:11.467: E/AndroidRuntime(530): at java.lang.reflect.Method.invoke(Method.java:491)
09-06 14:12:11.467: E/AndroidRuntime(530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-06 14:12:11.467: E/AndroidRuntime(530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-06 14:12:11.467: E/AndroidRuntime(530): at dalvik.system.NativeStart.main(Native Method)
09-06 14:12:19.667: I/Process(530): Sending signal. PID: 530 SIG: 9
错误行(即:ImageAdapter 中的 162)
button.setOnClickListener(new View.OnClickListener() {
代码:
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_column, null);
}
// STATUS
TextView txtStatus = (TextView) convertView.findViewById(R.id.txtStatus);
txtStatus.setPadding(5, 0, 0, 0);
txtStatus.setText(MyArrList.get(position).get("Status"));
Button button = (Button) findViewById(R.id.btnUpdate);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String sUserID = MyArrList.get(position).get("UserID").toString();
Log.d(tag, "sAppointmentID :: " + sUserID);
String sAppointmentID = MyArrList.get(position).get("AppointmentID").toString();
Log.d(tag, "sAppointmentID :: " + sAppointmentID);
Intent newActivity = new Intent(RescheduleActivity.this, UpdateActivity.class);
newActivity.putExtra("UserID", sUserID);
newActivity.putExtra("AppointmentID", sAppointmentID);
startActivity(newActivity);
}
});
return convertView;
}
}