-1

我正在编写一个程序,我需要在列表视图的每一行中使用按钮,但是每次我得到空指针异常,为什么?

日志说:

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;              
    }                
}
4

4 回答 4

2

抱歉迟到了,是的,我同意@njzk2@Raghunandan

我的意思是你刚刚错过了,你必须在你的适配器中使用convertView ,就像你在程序中使用其他小部件一样,

所以你的代码应该是这样的:

 Button button = (Button)convertView.findViewById(R.id.btnUpdate);
于 2013-09-06T09:03:59.183 回答
2

convertView.找到按钮时您错过了 :

代替

Button button = (Button) findViewById(R.id.btnUpdate);

Button button = (Button) convertView.findViewById(R.id.btnUpdate);
于 2013-09-06T08:55:58.000 回答
1

初始化如下

Button button = (Button)convertView.findViewById(R.id.btnUpdate);

你正在夸大一个视图。所以使用相同的findViewById

还可以使用视图支架来实现平滑滚动和性能

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

于 2013-09-06T08:56:19.437 回答
0

btnUpdate 的 xml 文件中有错误

于 2013-09-06T08:59:10.610 回答