按钮的onclickListener显示nullpointer异常

时间:2013-04-11 07:20:07

标签: android onclicklistener

这是我的代码:

holder.bookbtn=(Button)view.findViewById(R.id.bookbtn);
bookbtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 book.startActivity(new Intent(book,PaymentForm.class));



            }
        });

这是错误消息:

04-11 12:35:41.271: E/AndroidRuntime(941): FATAL EXCEPTION: main 04-11
12:35:41.271: E/AndroidRuntime(941): java.lang.NullPointerException
04-11 12:35:41.271: E/AndroidRuntime(941):  at
com.example.hotelsearch.NewsRowAdapter.getView(NewsRowAdapter.java:156)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.AbsListView.obtainView(AbsListView.java:2033) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.widget.ListView.onMeasure(ListView.java:1127) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.view.View.measure(View.java:12724) 04-11 12:35:41.271:
E/AndroidRuntime(941):  at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.view.View.measure(View.java:12724) 04-11 12:35:41.271:
E/AndroidRuntime(941):  at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.view.View.measure(View.java:12724) 04-11 12:35:41.271:
E/AndroidRuntime(941):  at
android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1163)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.widget.ScrollView.onMeasure(ScrollView.java:312) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.view.View.measure(View.java:12724) 04-11 12:35:41.271:
E/AndroidRuntime(941):  at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.view.View.measure(View.java:12724) 04-11 12:35:41.271:
E/AndroidRuntime(941):  at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.view.View.measure(View.java:12724) 04-11 12:35:41.271:
E/AndroidRuntime(941):  at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
android.view.View.measure(View.java:12724) 04-11 12:35:41.271:
E/AndroidRuntime(941):  at
android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-11 12:35:41.271: E/AndroidRuntime(941):  at
android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 04-11
12:35:41.271: E/AndroidRuntime(941):    at
com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)

3 个答案:

答案 0 :(得分:0)

 holder.bookbtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                 book.startActivity(new Intent(book,PaymentForm.class));
            }
        });

答案 1 :(得分:0)

如果您添加日志记录,例如

Log.d("~~~~~","\n\n\n~~~~ view="+view);
holder.bookbtn=(Button)view.findViewById(R.id.bookbtn);
Log.d("~~~~~","~~~~ holder.bookbtn="+holder.bookbtn);
holder.bookbtn.setOnClickListener(new View.OnClickListener() { ...

您很可能会发现holder.bookbtnnull,并且必须检查R.id.bookbtn是否真的存在于创建view中的视图的xml中

(我假设holder.bookbtn和bookbtn在你的代码的上下文中是相同的)

答案 2 :(得分:0)

这是我的补充getview方法

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;



    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(row, null);

        holder = new ViewHolder();
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    if ((items == null) || ((position + 1) > items.size()))
        return view;

    objBean = items.get(position);

    if(Details.getflag())
    {
        //objBean = items.get(position);

    //holder.tvName = (TextView) view.findViewById(R.id.HotelPlace);
    holder.HotelName = (TextView) view.findViewById(R.id.HotelName);
    holder.BasePrice = (TextView) view.findViewById(R.id.BasePrice);
    holder.NoOfRooms=(TextView)view.findViewById(R.id.FreeRoams);
    holder.RegularPrice=(TextView)view.findViewById(R.id.RegularPrice);
    holder.image = (ImageView) view.findViewById(R.id.HotelImg);
    //holder.RegularPrice.setPaintFlags(holder.RegularPrice.getPaintFlags()|Paint.STRIKE_THRU_TEXT_FLAG);

    //String url = "http://test.searchmyhotel.in/PropertyImages/1/"+objBean.getImageUrl();
    /*if (holder.tvName != null && null != objBean.getAddress()
            && objBean.getAddress().trim().length() > 0) {
        holder.tvName.setText(Html.fromHtml(objBean.getAddress()));*/

    //}

    if (holder.HotelName!= null && null != objBean.getPropertyName()
            && objBean.getPropertyName().trim().length() > 0) {
        holder.HotelName.setText(Html.fromHtml(objBean.getPropertyName()+" | "+objBean.getAddress()));
    }
     //Details.setpos(objBean.getAddress());
    if (holder.BasePrice != null && objBean.getBasePrice() >= 0) {
        holder.BasePrice.setText(Html.fromHtml("" + objBean.getBasePrice()+"Rs"));
    }
    if (holder.RegularPrice != null && objBean.getRegularPrice() >= 0) {
        holder.RegularPrice.setText(Html.fromHtml("" + objBean.getRegularPrice()+"Rs"));
    }
    if (holder.NoOfRooms != null && objBean.getFreeRoams() >= 0) {
        holder.NoOfRooms.setText(Html.fromHtml("" + objBean.getFreeRoams()));
    }


    /*new DisplayImageFromURL(holder.image).execute("http://api.androidhive.info/music/images/adele.png");
    holder.image.setImageBitmap(objBean.getImage());*/

    imageLoader.DisplayImage("http://api.androidhive.info/music/images/adele.png",holder.image);
    }
    else
    {


    holder.bookbtn=(Button)view.findViewById(R.id.bookbtn);
    holder.RoomType=(TextView)view.findViewById(R.id.RoomType);
    holder.HotelName = (TextView) view.findViewById(R.id.HotelName);
    holder.TypePrice=(TextView)view.findViewById(R.id.TypePrice);   
    holder.TypePriceDt=(TextView)view.findViewById(R.id.RoomTypePrice);
    holder.RoomTypeDt=(TextView)view.findViewById(R.id.HotelTypeDetailed);




    if (holder.HotelName!= null && null != objBean.getPropertyName()
            && objBean.getPropertyName().trim().length() > 0) {
        holder.HotelName.setText(Html.fromHtml(objBean.getPropertyName()));
    }

    if(holder.RoomType!=null && objBean.getRoomType().trim().length()>0){
    holder.RoomType.setText(Html.fromHtml(objBean.getRoomType()));}

    if(holder.RoomTypeDt!=null && objBean.getRoomType().trim().length()>0){
        holder.RoomTypeDt.setText(Html.fromHtml(objBean.getRoomType()));}

    if(holder.TypePrice!=null && objBean.getRoomPrice().trim().length()>=0){
    holder.TypePrice.setText(Html.fromHtml(objBean.getRoomPrice()));
    }

    if(holder.TypePriceDt!=null && objBean.getRoomPrice().trim().length()>=0){
        holder.TypePriceDt.setText(Html.fromHtml(objBean.getRoomPrice()));
        }
    holder.bookbtn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
             book.startActivity(new Intent(book,PaymentForm.class));



        }
    });


    }

    return view;
}