尝试在 ViewPager 中为我的 ImageView 设置图像资源时出现“找不到源”错误。但是如果我在 XML 中设置图像,我没有错误。但是,如果我在 XML 中设置图像,则在运行应用程序时不会显示它。我正在使用viewpagerindictor库和 android-support-v13.jar。活动代码
public class DataActivity extends Activity {
/** Called when the activity is first created. */
int obj_id, type_id, node_id;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
Bundle ex = getIntent().getExtras();
if (ex != null) {
obj_id = ex.getInt("obj");
type_id = ex.getInt("type");
node_id = ex.getInt("node");
}
LayoutInflater inf = LayoutInflater.from(this);
List<View> pages = new ArrayList<View>();
View page = inf.inflate(R.layout.page, null);
ImageView im = (ImageView)page.findViewById(R.id.im_v_obj1);
im.setImageDrawable(getResources().getDrawable(R.drawable.obj1));
pages.add(page);
PageAdapter pa = new PageAdapter(pages);
ViewPager vp = (ViewPager)findViewById(R.id.viewpager);
TitlePageIndicator tpi = (TitlePageIndicator)findViewById(R.id.indicator);
vp.setAdapter(pa);
tpi.setViewPager(vp);
}
}
页面适配器代码
public class PageAdapter extends PagerAdapter {
List<View> pages = null;
private static String[] titles = new String[]
{
"Object 1"
};
public PageAdapter(List<View> pages) {
this.pages = pages;
}
public String getPageTitle( int position )
{
return titles[ position ];
}
@Override
public Object instantiateItem(View collection, int pos) {
View v = pages.get(pos);
((ViewPager)collection).addView(v, 0);
return v;
}
@Override
public void destroyItem (View collection, int pos, Object view) {
((ViewPager)collection).removeView((View)view);
}
@Override
public int getCount() {
return pages.size();
}
@Override
public boolean isViewFromObject(View v, Object obj) {
return v.equals(obj);
}
XML 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>