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();
}
});
}
}
答案 0 :(得分:1)
按ID查找视图返回null。请在下面的行之后检查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();
}
});
}