图像滑块正常工作,直到我不在图像中加载图像。那就是:
int imageId[]={
};
但是一旦我这样做了:
int imageId[]={
R.drawable.mi
};
让我们知道我有一个有图像的活动A.现在,如果我长按该图像,另一个活动B应该开始。但Android应用程序突然关闭。
这是我的活动代码:
package com.example.hotel_app_regularuser;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class PageTwo extends Activity {
ViewPager vpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_two);
vpager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter adapter = new customAdapter(PageTwo.this);
vpager.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.page_two, menu);
return true;
}
}
class customAdapter extends PagerAdapter {
Context context;
int imageId[] = {
R.drawable.mi
};
public customAdapter(Context context) {
this.context = context;
}
public Object InstantiateItem(ViewGroup container, int item) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View viewitem = inflater.inflate(R.layout.activity_page_two, container, false);
ImageView im = (ImageView) viewitem.findViewById(R.id.imageView1);
im.setImageResource(imageId[item]);
TextView textView1 = (TextView) viewitem.findViewById(R.id.textView1);
textView1.setText("hi");
((ViewPager) container).addView(viewitem);
return viewitem;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == ((View) arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
}
答案 0 :(得分:1)
更改:
public Object InstantiateItem(ViewGroup container, int item) {
到:
@Override
public Object instantiateItem(ViewGroup container, int position) {
在您的代码中,它将解决问题。
即添加@Override
并将方法名称从instantiateItem
更改为InstantiateItem
答案 1 :(得分:0)
只需添加@Override
关键字,就像我在InstantiateItem()
方法中添加的一样。
@Override
public Object InstantiateItem(ViewGroup container, int item) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View viewitem = inflater.inflate(R.layout.activity_common_preferences, container, false);
ImageView im = (ImageView) viewitem.findViewById(R.id.imageView1);
im.setImageResource(imageId[item]);
TextView textView1 = (TextView) viewitem.findViewById(R.id.textView1);
textView1.setText("hi");
((ViewPager) container).addView(viewitem);
return viewitem;
}
答案 2 :(得分:0)
我刚刚发现这一行:
View viewitem=inflater.inflate(R.layout.activity_page_two,container,false);
我在写:
View `viewitem=inflater.inflate(R.layout.activity_commonpreferences,container,false);`
另外,正如@ Ashish Ranjan指出的那样,我改变了
public Object InstantiateItem(ViewGroup container, int item) {
到
@Override
public Object instantiateItem(ViewGroup container, int position) {
更改这些有助于顺利运行我的应用程序。