viewpager.draw上的Android java null

时间:2012-11-07 18:24:27

标签: android nullpointerexception android-viewpager

我无法理解错误的位置,因为它似乎不是来自我的应用程序的问题,而且似乎是自己的支持库。

我刚才意识到这个bug只发生在安装了android 2.1 update 1和有时候知道android 2.2 froyo的旧设备上。在我的设备Android 2.3及以上没有任何问题。我无法使用任何设备重现此问题,甚至使用配置了相同规格的手机的仿真器来解决此问题

我一整天都没有找到问题。

java.lang.NullPointerException
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1366)
        at android.view.View.draw(View.java:6743)
        at android.support.v4.view.ViewPager.draw(SourceFile:1923)
        at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
        at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
        at android.view.View.draw(View.java:6743)
        at android.widget.FrameLayout.draw(FrameLayout.java:352)
        at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
        at android.view.View.draw(View.java:6743)
        at android.widget.FrameLayout.draw(FrameLayout.java:352)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1843)
        at android.view.ViewRoot.draw(ViewRoot.java:1407)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
        at dalvik.system.NativeStart.main(Native Method)

来自Main Activity onCreate的代码

  • 线性布局寻呼机只是一组点,用于指示用户所在的寻呼机号码。填充寻呼机是用ViewPager getcount()重新获取的观看者数量点填充下部屏幕

  • PageListener只是检查viewpager何时作为切换页面并用另一种颜色绘制相应的点

    pager = (LinearLayout) findViewById(R.id.pager);
    
    appViewPager = (ViewPager) findViewById(R.id.AppViewPager);
    
    adapter = new MyPagerAdapter(this, appsList);
    
    appViewPager.setAdapter(adapter);
    
    populatePager();
    
    PageListener pageListener = new PageListener();
    
    appViewPager.setOnPageChangeListener(pageListener);
    

和MyPagerAdapter代码:

public class MyPagerAdapter extends PagerAdapter implements OnCancelListener {

private Context ctx = null;
public ArrayList<App> appsList;

public MyPagerAdapter(Context ctx, ArrayList<App> appsList) {
    this.ctx = ctx;
    this.appsList = appsList;
}

@Override
public int getCount() {
    int listsize = appsList.size();
    int numOfAppsPerPage = Integer.valueOf(ctx.getResources().getString(R.string.grid_number_of_apps_per_page));
    float num = Float.valueOf(listsize) / Float.valueOf(numOfAppsPerPage);
    float rest = num % 1;

    int numOfPagers = Math.round(num - rest);

    if (rest > 0 || numOfPagers == 0) {
        numOfPagers = numOfPagers + 1;
    }

    return numOfPagers;
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

@Override
public Object instantiateItem(View collection, int position) {

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View view = inflater.inflate(R.layout.grid, null);

    GridView gridview = (GridView) view.findViewById(R.id.gridview);

    int starterPosition;

    int numOfAppsPerPage = Integer.valueOf(ctx.getResources().getString(R.string.grid_number_of_apps_per_page));

    starterPosition = numOfAppsPerPage * position;
    int enderPosition = starterPosition + numOfAppsPerPage;

    // Log.e("ViewPagerPosition", "Position:" + position);

    int appsListSize = appsList.size();

    if (enderPosition > appsListSize) {
        enderPosition = appsListSize;
    }

    ArrayList<App> shorterList = new ArrayList<App>(appsList.subList(
            starterPosition, enderPosition));

    final AppLauncherAdapter grid = new AppLauncherAdapter(ctx, shorterList);

    gridview.setAdapter(grid);

    ((ViewPager) collection).addView(gridview, 0);

    return gridview;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
    ((ViewPager) arg0).removeView((View) arg2);

}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题并通过此链接解决了问题。 我希望这会有所帮助:

https://vvirlan.wordpress.com/2015/01/11/solved-android-java-lang-nullpointerexception-at-android-view-viewgroup-dispatchdrawviewgroup-java2922/