我在片段
中制作了自定义gridview首先我在logcat上显示错误
01-30 19:01:02.266: E/AndroidRuntime(15600): FATAL EXCEPTION: main
01-30 19:01:02.266: E/AndroidRuntime(15600): Process: com.example.achessapp, PID: 15600
01-30 19:01:02.266: E/AndroidRuntime(15600): java.lang.NullPointerException
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.AbsListView.obtainView(AbsListView.java:2426)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.GridView.onMeasure(GridView.java:1044)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.View.measure(View.java:16980)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:824)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:500)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.View.measure(View.java:16980)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.View.measure(View.java:16980)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.LinearLayout.measureVertical(LinearLayout.java:926)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.LinearLayout.onMeasure(LinearLayout.java:607)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.View.measure(View.java:16980)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.View.measure(View.java:16980)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
01-30 19:01:02.266: E/AndroidRuntime(15600): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.View.measure(View.java:16980)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5374)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
01-30 19:01:02.266: E/AndroidRuntime(15600): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2343)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.View.measure(View.java:16980)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2252)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1315)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1192)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6231)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:816)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.Choreographer.doCallbacks(Choreographer.java:619)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.Choreographer.doFrame(Choreographer.java:588)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:802)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.os.Handler.handleCallback(Handler.java:808)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.os.Handler.dispatchMessage(Handler.java:103)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.os.Looper.loop(Looper.java:193)
01-30 19:01:02.266: E/AndroidRuntime(15600): at android.app.ActivityThread.main(ActivityThread.java:5395)
01-30 19:01:02.266: E/AndroidRuntime(15600): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 19:01:02.266: E/AndroidRuntime(15600): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 19:01:02.266: E/AndroidRuntime(15600): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
01-30 19:01:02.266: E/AndroidRuntime(15600): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
01-30 19:01:02.266: E/AndroidRuntime(15600): at dalvik.system.NativeStart.main(Native Method)
java代码是
class UniteFragment extends Fragment implements OnClickListener {
View mView;
Activity mActivity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.unite_screen, container, false);
GridView mGridUnite = (GridView) mView
.findViewById(R.id.grdGrupOfUnite);
mGridUnite.setAdapter(new MyBaseAdapter());
return mView;
}
@Override
public void onAttach(Activity activity) {
mActivity = activity;
super.onAttach(activity);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
class MyBaseAdapter extends BaseAdapter {
LayoutInflater mInflator;
public MyBaseAdapter() {
mInflator = mActivity.getLayoutInflater();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflator.inflate(R.layout.unite_grup_item, null);
viewHolder = new ViewHolder();
viewHolder.ivUser = (CircularImageView) convertView
.findViewById(R.id.ivGrupIcon);
viewHolder.tvCounter = (TextView) convertView
.findViewById(R.id.tvRedCounter);
viewHolder.tvName = (TextView) convertView
.findViewById(R.id.tvName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.ivUser.setImageResource(R.drawable.unite_item);
viewHolder.tvCounter.setText("" + position);
viewHolder.tvName.setText("User " + position);
return null;
}
class ViewHolder {
CircularImageView ivUser;
TextView tvCounter;
TextView tvName;
int position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
}
R.layout.unit_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:src="@drawable/unite_bg" />
<GridView
android:id="@+id/grdGrupOfUnite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3" >
</GridView>
</RelativeLayout>
unite_grup_item.xml是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.achessapp.widget.CircularImageView
android:id="@+id/ivGrupIcon"
android:layout_width="@dimen/icon_size"
android:layout_height="@dimen/icon_size"
android:background="@android:color/white"
android:src="@drawable/unite_item" />
<TextView
android:id="@+id/tvRedCounter"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:background="@drawable/red_counter_shape"
android:text="1" />
<TextView
android:id="@+id/tvName"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="1"
android:textColor="@android:color/black" />
</RelativeLayout>
如果有人能解决这个问题,请帮我找出解决方案
答案 0 :(得分:3)
从convertView
方法
null
准备的getView