滚动(垂直)后单击时,出现空指针异常,而不是TextView。
供您参考,我使用以下代码:

collection_image.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <GridView
    android:id="@+id/gridView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="3"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="120dp"
    android:gravity="center" />
</RelativeLayout>


adapter_griditem.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:gravity="center" >

   <TextView
       android:id="@+id/grid_item_text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:gravity="center_horizontal"
       android:textColor="#FFFFFF" />

</LinearLayout>


扩展BaseAdapter类的ImageAdapter的getView函数

public View getView(int position, View convertView, ViewGroup parent) {
    View MyView = convertView;
    try{
        Image cached = images[position];
        if(convertView == null) {
            MyView = LayoutInflater.from(mContext).inflate(R.layout.adapter_griditem, null);
        }
        title = (TextView)MyView.findViewById(R.id.grid_item_text);

        if(cached.thumb == null) {

            // no cached thumb,

        } else {
            title.setTag(images[position].fullPath);
            title.setText(imageCollection[position]);
            title.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(cached.thumb), null, null);
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
    return MyView;
}


活动时(两个功能):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.collection_image, container, false);
}

@Override
public void onResume(){
    imageAdapter = new ImageAdapter(getActivity().getApplicationContext(), collectionCount, mTag, imageCollection, mType);

        View fragmentView = getView();
        mGridView = (GridView) fragmentView.findViewById(R.id.gridView);
        mGridView.destroyDrawingCache();
        mGridView.setAdapter(imageAdapter);

        mGridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                TextView mtext = (TextView) parent.getChildAt(position).findViewById(R.id.grid_item_text);
                if(mtext != null){
                    String imageURL = (String) mtext.getTag();
                    gridHandler(imageURL);
                }
            }
        });

}


任何人都可以吗?

编辑1:

Logcat ===

05-30 18:17:23.371: E/AndroidRuntime(512): FATAL EXCEPTION: main
05-30 18:17:23.371: E/AndroidRuntime(512): java.lang.NullPointerException
05-30 18:17:23.371: E/AndroidRuntime(512):  at com.tftus.greetz.fragments.DisplayCardsFragment$1.onItemClick(DisplayCardsFragment.java:71)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.widget.AdapterView.performItemClick(AdapterView.java:282)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.widget.AbsListView.performItemClick(AbsListView.java:1032)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2447)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.widget.AbsListView$1.run(AbsListView.java:3065)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.os.Handler.handleCallback(Handler.java:587)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.os.Looper.loop(Looper.java:126)
05-30 18:17:23.371: E/AndroidRuntime(512):  at android.app.ActivityThread.main(ActivityThread.java:3997)
05-30 18:17:23.371: E/AndroidRuntime(512):  at java.lang.reflect.Method.invokeNative(Native Method)
05-30 18:17:23.371: E/AndroidRuntime(512):  at java.lang.reflect.Method.invoke(Method.java:491)
05-30 18:17:23.371: E/AndroidRuntime(512):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
05-30 18:17:23.371: E/AndroidRuntime(512):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
05-30 18:17:23.371: E/AndroidRuntime(512):  at dalvik.system.NativeStart.main(Native Method)


“ DisplayCardsFragment.java:71”的代码

TextView mtext =(TextView)parent.getChildAt(position).findViewById(R.id.grid_item_text);

最佳答案

我认为它没有得到XML文件的引用,所以空指针异常

关于android - 滚动后在选择网格项上获取空指针异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10816870/

10-09 07:32