这是我设置listview项目点击监听器的代码。
listview.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent it=new Intent(ProfileView.this,View_profile.class);
it.putExtra("position", position);
startActivity(it);
}
});
这是我想在点击listview项目后打开的另一个活动。 这是(View_profile Activity)中的代码。
公共类View_profile扩展ActionBarActivity实现BaseSliderView.OnSliderClickListener {
private SliderLayout mDemoSlider;
String value="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
// int prePosition = getIntent().getIntExtra("position", 0);
// System.out.println(""+prePosition);
Intent i=getIntent();
value=i.getStringExtra("position");
System.out.println(""+value);
mDemoSlider = (SliderLayout)findViewById(R.id.slider_view);
HashMap<String,String> url_maps = new HashMap<String, String>();
url_maps.put("Hannibal", "http://static2.hypable.com/wp-content/uploads/2013/12/hannibal-season-2-release-date.jpg");
url_maps.put("Big Bang Theory", "http://tvfiles.alphacoders.com/100/hdclearart-10.png");
url_maps.put("House of Cards", "http://cdn3.nflximg.net/images/3093/2043093.jpg");
url_maps.put("Game of Thrones", "http://images.boomsbeat.com/data/images/full/19640/game-of-thrones-season-4-jpg.jpg");
HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
file_maps.put("Hannibal",R.drawable.hannibal);
file_maps.put("Big Bang Theory",R.drawable.bigbang);
file_maps.put("House of Cards",R.drawable.house);
file_maps.put("Game of Thrones", R.drawable.game_of_thrones);
for(String name : file_maps.keySet()){
TextSliderView textSliderView = new TextSliderView(this);
// initialize a SliderLayout
textSliderView
.description(name)
.image(file_maps.get(name))
.setScaleType(BaseSliderView.ScaleType.Fit)
.setOnSliderClickListener(this);
//add your extra information
textSliderView.getBundle()
.putString("extra",name);
mDemoSlider.addSlider(textSliderView);
}
mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
mDemoSlider.setCustomAnimation(new DescriptionAnimation());
mDemoSlider.setDuration(4000);
ListView l = (ListView)findViewById(R.id.transformers);
l.setAdapter(new TransformerAdapter(this));
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDemoSlider.setPresetTransformer(((TextView) view).getText().toString());
Toast.makeText(View_profile.this, ((TextView) view).getText().toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onSliderClick(BaseSliderView slider) {
// TODO Auto-generated method stub
}
答案 0 :(得分:1)
getStringExtra()
可能会失败
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Intent it=new Intent(ProfileView.this,View_profile.class);
it.putIntExtra("position", position);
ProfileView.this.startActivity(it);
}
});
并在接收端
Intent i=getIntent();
value=i.getIntExtra("position");//your passing position as int
答案 1 :(得分:0)
我认为你在某处获得了NullPointerException。
请提供堆栈跟踪,因为错误可能发生在setOnItemClickListener内部或View_profile活动内部。