我正在尝试创建一个首先显示包含许多项目的ListView
的应用,当用户点击特定项目时,下一个活动将打开以显示带有ViewPager
的图片。
一开始我创建了ViewPager
,我可以在图片之间滑动,但是当我尝试创建一个新的其他应用,首先显示ListView
并点击其项目后应用程序ViewPager
停止并显示此消息:“遗憾的是已停止”。应用程序开始在列表中显示项目,但是当用户点击应用程序停止工作我试图用 LogCat 找到错误,但它没有显示任何我不知道为什么!!!
主要课程
public class MainActivity extends Activity implements OnItemClickListener{
ListView itemListView;
String[] values;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemListView=(ListView)findViewById(R.id.itemListView);
values=getResources().getStringArray(R.array.values);
itemListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,values));
itemListView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent i=new Intent(MainActivity.this,SwipeActivity.class);
switch(itemListView.getItemAtPosition(position).toString()){
case "First":
i.putExtra("nextActivity", 1);
startActivity(i);
break;
case "Second":
i.putExtra("nextActivity", 2);
startActivity(i);
break;
}
}
}
SwipeActivity“类
public class SwipeActivity extends Activity{
ViewPager vp;
Table_customSwipeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
vp=(ViewPager)findViewById(R.id.view_pager);
adapter=new Table_customSwipeAdapter(this);
vp.setAdapter(adapter);
}
}
Table_customSwipeAdapter“class
public class Table_customSwipeAdapter extends PagerAdapter{
private int[] image_resources={R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5};
private Context ctx;
private LayoutInflater layoutInflater;
public Table_customSwipeAdapter(Context ctx) {
this.ctx=ctx;
}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return (arg0==(LinearLayout)arg1);
}
public Object instantiateItem(ViewGroup container, int position){
layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view=layoutInflater.inflate(R.layout.activity_table_custom_swipe_adapter, container,false);
ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
TextView textView=(TextView)item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText("Image "+(position+1));
container.addView(item_view);
return item_view;
}
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout)object);
}
}
我认为在调用Table_customSwipeAdapter
课程时会抛出异常,但在添加ListView
之前我不知道应用为什么会有效?