本來很簡(jiǎn)單的一個(gè)東西弄了幾個(gè)小時(shí),好吧記錄一下遇到的種種問題,以便以后出現(xiàn)同樣的錯(cuò)誤可以迅速解決
ViewPager是android3.0中新加的一個(gè)組件,用來右滑動(dòng)切換多個(gè)View,這東西很適合做啟動(dòng)頁(yè)面與導(dǎo)航頁(yè)面之類的東西.
要使用ViewPager首先需要做如下事情:
1.導(dǎo)入android-support-v4.jar
2.一個(gè)包含ViewPager的布局文件
3.最后制作一個(gè)每個(gè)切換的View的Layout文件,infalte起來比較方便(可選)
4.實(shí)現(xiàn)一個(gè)Adapter 繼承與 PagerAdapter ,在
instantiateItem
方法中生成View,并將這個(gè)view添加到container中 . container.addView(v)
PagerAdapter必須實(shí)現(xiàn)如下方法: (帶星號(hào)的是必須實(shí)現(xiàn)的方法)
* public int getCount() 獲取總View的數(shù)量
public void startUpdate(ViewGroup container)
* public Object instantiateItem(ViewGroup container, int position) 實(shí)例化當(dāng)前的View,添加到contrainer中,然后返回生成的View或關(guān)聯(lián)的Object (用于在isViewFromObject中檢查)
* public void destroyItem(ViewGroup container,
int position, Object object) 從container中移除指定的View (釋放內(nèi)存)
public void finishUpdate(ViewGroup container)
* public boolean isViewFromObject(View view, Object object) 檢查View是否與一個(gè)Object關(guān)聯(lián),
在開發(fā)過程中遇到很多問題總結(jié)如下:
1.通過gradle 添加編譯依賴包 ,由兩種方式可以添加
(第一種導(dǎo)入方式)通過jar包放入到lib目錄下,然后添加
compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:android-support-v7-appcompat' compile 'com.android.support:android-support-v4'
(第二種導(dǎo)入方式)通過sdk目錄下 android-sdk-linux/extras/android/m2repository/com/android/support/support-v4/ 目錄下對(duì)應(yīng)的版本導(dǎo)入
compile 'com.android.support:support-v4:22.2.0' compile 'com.android.support:appcompat-v7:22.2.0'
導(dǎo)入完整后編譯總是提示
Unable to start activity ComponentInfo{linuxeye.com.knowledgeispower/linuxeye.com.knowledgeispower.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
開始以為是因?yàn)榘鼪]有導(dǎo)入,到處google,bing,baidu, 看到一片stackoverflow上的問答,說是Activity沒有生成,突然間想到是不是Activity的代碼有問題.
經(jīng)過反復(fù)調(diào)試,發(fā)現(xiàn)findViewById竟然在setViewContent之前被調(diào)用,導(dǎo)致findViewById一致返回null引起的.我靠這個(gè)錯(cuò)誤的代價(jià)是一個(gè)小時(shí)!這個(gè)一個(gè)小時(shí)反復(fù)的清理項(xiàng)目從新導(dǎo)入包..反復(fù)的檢查問題.
之間還想過是不是ViewPager的布局外面需要一個(gè)Layout之類的.真的是累死人了.ViewPager是不需要外容器的,可以直接作為L(zhǎng)ayout使用的.
布局文件如下 . mypager.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager android:id="@+id/my_pager" xmlns:android="http://schemas./apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > </android.support.v4.view.ViewPager>
修改完Activity后可以findViewById了,接下來的問題就是Activity確實(shí)顯示出來了,就是什么內(nèi)容都沒有,一片漆黑呀! 這個(gè)過程也是浪費(fèi)了我好長(zhǎng)時(shí)間,反復(fù)baidu看別人的代碼和文章,發(fā)現(xiàn)一個(gè)問題,我的isViewFromObject方法是android Studio自動(dòng)生成的,代碼自動(dòng)返回false,看到文檔里寫著這個(gè)方法是用來比較View和Object是否有關(guān)聯(lián)? 這個(gè)方法是在ViewPager中被調(diào)用的,ViewPager將每一個(gè)View添加到mItems中,mItem是一個(gè)ArrayList<ItemInfo> , ItemInfo是每個(gè)View相關(guān)的信息,其中Object就是 instantiateItem 方法返回的哪個(gè)Object,
官方文檔標(biāo)注這個(gè)對(duì)象可以不是一個(gè)View,是用來讓ViewPager檢查是否和一個(gè)View對(duì)應(yīng)的.
static class ItemInfo { Object object; int position; boolean scrolling; float widthFactor; float offset; }
修改了isViewFromObject方法為:
public boolean isViewFromObject(View view, Object object) { return view == object; }
一切都o(jì)k了,圖片終于顯示出來了!大工告成!
AppPagerAdapter代碼如下:
package linuxeye.com.knowledgeispower;
import android.content.Context; import android.content.Intent; import android.support.v4.view.PagerAdapter; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.ImageView;
import java.util.ArrayList;
/** * Created by Raffeale on 15-7-7. */ public class AppPagerAdapter extends PagerAdapter {
Context ctx = null; ArrayList<ImageView> pagerData = null;
public AppPagerAdapter(Context ct , ArrayList<ImageView> data) {
ctx = ct; pagerData = data;
}
/** * Return the number of views available. */ @Override public int getCount() { return pagerData.size(); }
/** * Create the page for the given position. The adapter is responsible * for adding the view to the container given here, although it only * must ensure this is done by the time it returns from * {@link #finishUpdate(ViewGroup)}. * * @param container The containing View in which the page will be shown. * @param position The page position to be instantiated. * @return Returns an Object representing the new page. This does not * need to be a View, but can be some other container of the page. */ @Override public Object instantiateItem(ViewGroup container, int position) { Log.i("PagerAdapter" , "method instantiateItem be calling" ); View v = LayoutInflater.from(ctx).inflate(R.layout.pager , null); ImageView img = (ImageView) v.findViewById(R.id.page_img); img.setImageResource(R.drawable.pic0+position); if(position+1 == pagerData.size()) { ImageButton ib = (ImageButton)v.findViewById(R.id.ib); ib.setVisibility(View.VISIBLE); ib.setBackgroundResource(R.drawable.bt); ib.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ctx.startActivity(new Intent(ctx,ArticleActivity.class)); } }); }
container.addView(v); return v;
//return super.instantiateItem(container, position); }
/** * Remove a page for the given position. The adapter is responsible * for removing the view from its container, although it only must ensure * this is done by the time it returns from {@link #finishUpdate(ViewGroup)}. * * @param container The containing View from which the page will be removed. * @param position The page position to be removed. * @param object The same object that was returned by * {@link #instantiateItem(View, int)}. */ @Override public void destroyItem(ViewGroup container, int position, Object object) { Log.i("PagerAdapter" , "method destroyItem be calling"); container.removeView((View)object); }
/** * Determines whether a page View is associated with a specific key object * as returned by {@link #instantiateItem(ViewGroup, int)}. This method is * required for a PagerAdapter to function properly. * * @param view Page View to check for association with <code>object</code> * @param object Object to check for association with <code>view</code> * @return true if <code>view</code> is associated with the key object <code>object</code> */ @Override public boolean isViewFromObject(View view, Object object) { return view == object; }
}
Activity代碼如下:
package linuxeye.com.knowledgeispower;
import android.app.Activity; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.widget.ImageView;
import java.util.ArrayList;
public class MainActivity extends Activity { ViewPager vp = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
//vp = new ViewPager(this); setContentView(R.layout.activity_main); ArrayList<ImageView> data = new ArrayList<ImageView>(); for(int i=0;i<5;i++) { ImageView img = new ImageView(this); img.setImageResource(R.drawable.pic0+i); data.add(img); }
AppPagerAdapter myadapter = new AppPagerAdapter(this , data);
vp = (ViewPager)findViewById(R.id.my_pager);
//LayoutInflater inflater = getLayoutInflater(); try{
vp.setAdapter(myadapter); }catch (Exception ex) { ex.printStackTrace(); }
}
}
|