1

我无法理解错误在哪里,因为它似乎不是源自我的应用程序的问题,并且似乎在它自己的支持库中。

我刚刚意识到这个错误只发生在带有 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;
}
}

有任何想法吗?

4

1 回答 1

0

我有一个类似的问题并用这个链接解决它。我希望这有帮助:

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

于 2015-02-05T17:33:24.697 回答