我正在尝试实现一个在android中使用带有gridview的viewpager片段的avtivity。每个gridview包含八个系统的应用程序图标,片段的数量是动态的。它运作良好,但滑到第三个片段。对不起我的英文。
在应用程序的当前状态中,我得到以下异常:
V/test_GridAdapter( 1418): The position is 0, com.android.providers.downloads.ui getView
V/test_GridAdapter( 1418): The position is 0, com.android.providers.downloads.ui getView
V/test_GridAdapter( 1418): The position is 0, com.android.providers.downloads.ui getView
V/test_GridAdapter( 1418): The position is 0, com.android.providers.downloads.ui getView
V/test_GridAdapter( 1418): The position is 0, com.android.providers.downloads.ui getView
V/test_GridAdapter( 1418): The position is 1, com.android.quicksearchbox getView
V/test_GridAdapter( 1418): The position is 2, com.rockchip.gamecontrolsetting getView
V/test_GridAdapter( 1418): The position is 3, com.android.apkinstaller getView
V/test_GridAdapter( 1418): The position is 4, com.android.rockchip getView
V/test_GridAdapter( 1418): The position is 5, com.android.speechrecorder getView
V/test_GridAdapter( 1418): The position is 6, com.android.videoeditor getView
V/test_GridAdapter( 1418): The position is 7, com.rockchip.wfd getView
V/test_GridAdapter( 1418): The position is 0, com.rockchip.mediacenter getView
V/test_GridAdapter( 1418): The position is 0, com.rockchip.mediacenter getView
V/test_GridAdapter( 1418): The position is 0, com.rockchip.mediacenter getView
V/test_GridAdapter( 1418): The position is 0, com.rockchip.mediacenter getView
V/test_GridAdapter( 1418): The position is 0, com.rockchip.mediacenter getView
D/AndroidRuntime( 1418): Shutting down VM
W/dalvikvm( 1418): threadid=1: thread exiting with uncaught exception (group=0x41c79300)
E/AndroidRuntime( 1418): FATAL EXCEPTION: main
E/AndroidRuntime( 1418): Process: com.example.zhengjy.ljlauncher, PID: 1418
E/AndroidRuntime( 1418): java.lang.NullPointerException
E/AndroidRuntime( 1418): at com.example.zhengjy.ljlauncher.GridAdapter.getView(GridAdapter.java:95)
E/AndroidRuntime( 1418): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
E/AndroidRuntime( 1418): at android.widget.GridView.makeAndAddView(GridView.java:1345)
E/AndroidRuntime( 1418): at android.widget.GridView.makeRow(GridView.java:345)
E/AndroidRuntime( 1418): at android.widget.GridView.fillDown(GridView.java:287)
E/AndroidRuntime( 1418): at android.widget.GridView.fillFromTop(GridView.java:421)
E/AndroidRuntime( 1418): at android.widget.GridView.layoutChildren(GridView.java:1233)
E/AndroidRuntime( 1418): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1626)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E/AndroidRuntime( 1418): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E/AndroidRuntime( 1418): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
E/AndroidRuntime( 1418): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
E/AndroidRuntime( 1418): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
E/AndroidRuntime( 1418): at android.view.View.layout(View.java:14838)
E/AndroidRuntime( 1418): at android.view.ViewGroup.layout(ViewGroup.java:4631)
E/AndroidRuntime( 1418): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1996)
E/AndroidRuntime( 1418): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1753)
E/AndroidRuntime( 1418): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1005)
E/AndroidRuntime( 1418): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5750)
E/AndroidRuntime( 1418): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
E/AndroidRuntime( 1418): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
E/AndroidRuntime( 1418): at android.view.Choreographer.doFrame(Choreographer.java:544)
E/AndroidRuntime( 1418): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
E/AndroidRuntime( 1418): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime( 1418): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 1418): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 1418): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime( 1418): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1418): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 1418): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
E/AndroidRuntime( 1418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
E/AndroidRuntime( 1418): at dalvik.system.NativeStart.main(Native Method)
activiy xml是:
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="1dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/pagerIndicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="5dp"
android:layout_weight="5" />
grid.xml是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:layout_marginTop="10dp"
android:horizontalSpacing="100dp"
android:listSelector="@android:color/transparent"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp" />
grid_item xml是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_grid_item"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="@dimen/app_margin">
<ImageView
android:id="@+id/app_icon"
android:layout_width="@dimen/grid_icon_width"
android:layout_height="@dimen/grid_icon_height"
android:layout_centerHorizontal="true"
android:visibility="visible" />
<RelativeLayout
android:id="@+id/label_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/app_icon"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:minWidth="20dp"
android:ellipsize="end"
android:singleLine="true"
android:padding="4dp"
android:textSize="15sp" />
</RelativeLayout>
GridAdapter是:
public class GridAdapter extends BaseAdapter {
Context context;
private GridItems[] items;
private LayoutInflater mInflater;
public class ViewHolder {
public ImageView imageView;
public TextView textTitle;
}
public GridAdapter(Context context, GridItems[] items) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.items = items;
}
...
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public Object getItem(int position) {
if (items != null && position >= 0 && position < getCount()) {
return items[position];
}
return null;
}
@Override
public long getItemId(int position) {
if (items != null && position >= 0 && position < getCount()) {
return items[position].id;
}
return 0;
}
public void setItemsList(GridItems[] locations) {
this.items = locations;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.app_grid_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = ((ImageView) convertView.findViewById(R.id.app_icon));
viewHolder.textTitle = ((TextView) convertView.findViewById(R.id.app_name));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
GridItems gridItems = items[position];
viewHolder.imageView.setImageDrawable(gridItems.appInfo.icon);
viewHolder.textTitle.setText(gridItems.appInfo.name);
Log.v(TAG, "The position is " + position + ", " + gridItems.appInfo.packageName + " getView");
return convertView;
}
}
FragmentStatePagerAdapter是:
private class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<GridFragment> fragments;
FragmentManager fm;
public MyPagerAdapter(FragmentManager fm, List<GridFragment> gridFragments) {
super(fm);
this.fm = fm;
this.fragments = gridFragments;
}
@Override
public GridFragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
}
答案 0 :(得分:0)
空指针似乎在您的getView()
方法中。使用以下内容确定哪个项为空:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.app_grid_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.imageView = ((ImageView) convertView.findViewById(R.id.app_icon));
viewHolder.textTitle = ((TextView) convertView.findViewById(R.id.app_name));
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (viewHolder == null) {
Log.e("GridAdapter", "viewHolder was null")
return convertView;
}
GridItems gridItems = items[position];
if (gridItems == null || gridItems.appInfo == null) {
Log.e("GridAdapter", "gridItems or appInfo was null")
return convertView;
}
viewHolder.imageView.setImageDrawable(gridItems.appInfo.icon);
viewHolder.textTitle.setText(gridItems.appInfo.name);
Log.v(TAG, "The position is " + position + ", " + gridItems.appInfo.packageName + " getView");
return convertView;
}