0
I'm working with carousel i just used the Caruosel source code from [codeProject][1] site 

一切都很好,直到我在运行应用程序时添加了 setOnItemClickListener 函数,它崩溃了如何解决这个崩溃以使我的应用程序工作???

import com.name.w.R;
    import com.name.controls.Carousel;
    import com.name.controls.CarouselAdapter;
    import com.name.controls.CarouselAdapter.OnItemClickListener;

    public class NameActivity extends Activity  {
        /** Called when the activity is first created. */


        Carousel care;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           care=(Carousel) findViewById(R.id.carousel);


            care.setOnItemClickListener(new OnItemClickListener(){
             public void onItemClick(CarouselAdapter<?> parent,View view,int position,long id){
                    Toast.makeText(NameActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show();               
                }





            });
        }
    }
4

1 回答 1

1

按 ID 查找视图返回 null。请在以下行之后检查是否为空。

care = (Carousel) findViewById(R.id.carousel);
    if (care != null) {
        care.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(CarouselAdapter<?> parent, View view,
                    int position, long id) {
                Toast.makeText(NameActivity.this, "Position=" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
于 2012-08-26T07:03:54.370 回答