如何在android中解决nullpointerexception?

时间:2014-01-06 12:51:36

标签: android

我收到NullPointerException代码,

package com.example.extra;

import java.util.List;

import com.example.dto.DriverSelectedRouteArrayList;
import com.example.main.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class CustomerAdapter extends BaseAdapter
{

    DriverSelectedRouteArrayList driverSelectedRouteArrayList;
    private List<DriverSelectedRouteArrayList> list;
    private LayoutInflater layoutInflater;
    Context context;
    ViewHolder holder;

    public CustomerAdapter(Context context, int resource, List<DriverSelectedRouteArrayList> driverData) 
    {
        this.list = driverData;
        this.context = context;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.student_list_view_itom, null);

        if(view == null)
        {
            TextView textViewRouteId = (TextView) view.findViewById(R.id.srNumberTextView);

//          convertView = layoutInflater.inflate(R.layout.student_list_view_itom, null);
            holder = new ViewHolder();
            holder.srNumberTextView = (TextView) convertView.findViewById(R.id.srNumberTextView);
            holder.nameTextView = (TextView) convertView.findViewById(R.id.textViewStudentItem);
            holder.phoneNumberTextView = (TextView) convertView.findViewById(R.id.textViewPhoneNumber);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxSelectForStudentList);
            holder.checkBox
                            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                                @Override
                                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
                                {
                                    int getPosition = (Integer) buttonView.getTag();
                                    list.get(getPosition).setSelected(buttonView.isChecked());

                                }
                            });
                            view.setTag(holder);
                            view.setTag(R.id.checkBoxSelectForStudentList,holder.checkBox);
                            view.setTag(R.id.srNumberTextView,holder.srNumberTextView);
                            view.setTag(R.id.textViewStudentItem, holder.nameTextView);
                            view.setTag(R.id.textViewPhoneNumber, holder.phoneNumberTextView);

        }else{
                holder = (ViewHolder) view.getTag();
             }
        holder.checkBox.setTag(position);//error in this line
        Log.e("get position=======>", ""+list.get(position).get_id());
        holder.srNumberTextView.setText(String.valueOf(list.get(position).get_id()));
        holder.nameTextView.setText(list.get(position).getFirst_name()+" "
                                    +list.get(position).getMiddle_name()+" "
                                    +list.get(position).getLast_name());
        holder.phoneNumberTextView.setText(list.get(position).getPhone_number());
        holder.checkBox.setChecked(list.get(position).isSelected());
        return view;
    }

    static class ViewHolder
    {
        protected TextView srNumberTextView;
        protected TextView nameTextView;
        protected TextView phoneNumberTextView;
        protected CheckBox checkBox;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}

标题LogCat:

01-06 07:42:53.132: E/AndroidRuntime(3300): FATAL EXCEPTION: main
01-06 07:42:53.132: E/AndroidRuntime(3300): Process: com.example.main, PID: 3300
01-06 07:42:53.132: E/AndroidRuntime(3300): java.lang.NullPointerException
01-06 07:42:53.132: E/AndroidRuntime(3300):     at com.example.extra.CustomerAdapter.getView(CustomerAdapter.java:73)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.widget.AbsListView.obtainView(AbsListView.java:2263)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.widget.ListView.onMeasure(ListView.java:1175)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.View.measure(View.java:16497)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.View.measure(View.java:16497)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.View.measure(View.java:16497)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.View.measure(View.java:16497)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.View.measure(View.java:16497)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.Choreographer.doFrame(Choreographer.java:544)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.os.Handler.handleCallback(Handler.java:733)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.os.Looper.loop(Looper.java:136)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at java.lang.reflect.Method.invokeNative(Native Method)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at java.lang.reflect.Method.invoke(Method.java:515)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-06 07:42:53.132: E/AndroidRuntime(3300):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:5)

更改为

  holder.srNumberTextView = (TextView) view.findViewById(R.id.srNumberTextView);

其他观点也一样。使用view.findViewById代替convertView.findViewById

 layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  // already initialized in the constructor of adapter class
  // no need to initialize in getView

你需要移动

 holder.checkBox.setOnCheckedChangeListener

如果部分

你有

  int getPosition = (Integer) buttonView.getTag();

甚至在你将标签设置为复选框之前。

可能还有其他错误。我指出了一些我可以睁开眼睛的事。

@Override
public View getView(int position, View convertView, ViewGroup parent)
{

    ViewHolder holder;
    if(convertView == null)
    {

        convertView = layoutInflater.inflate(R.layout.student_list_view_itom, null);
        holder = new ViewHolder();
        holder.textViewRouteId = (TextView) convertView.findViewById(R.id.srNumberTextView); 
        holder.srNumberTextView = (TextView) convertView.findViewById(R.id.srNumberTextView);
        holder.nameTextView = (TextView) convertView.findViewById(R.id.textViewStudentItem);
        holder.phoneNumberTextView = (TextView) convertView.findViewById(R.id.textViewPhoneNumber);
        holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxSelectForStudentList);
        convertView.setTag(holder);

    }else{
            holder = (ViewHolder) convertView.getTag();
         }

    // update your views here
    return convertView;
   }

然后

static class ViewHolder
{
    protected TextView srNumberTextView,textViewRouteId;