1

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

集合图像.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>

适配器_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:

日志猫===

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);

4

4 回答 4

1

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

TextView mtext = (TextView)v.findViewById(R.id.grid_item_text); 
于 2012-05-30T13:10:45.913 回答
1

换行试试

 TextView mtext = (TextView) parent.getChildAt(position).findViewById(R.id.grid_item_text); 
           by 
 TextView mtext = (TextView) v.findViewById(R.id.grid_item_text); 
于 2012-05-30T13:23:01.847 回答
1

OnItemClickListener本身提供itemview. 当您使用gridView.Child它时,它将仅返回当前项目,但并非所有这就是您遇到问题的原因.....

采用:

v.findViewById(R.id.itemid); 
于 2012-07-30T06:25:46.497 回答
1

你替换

TextView mtext = (TextView)v.findViewById(R.id.grid_item_text); 

经过

TextView mtext = (TextView)v;

在adapter_griditem.xml 中:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/grid_item_text"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:textColor="#FFFFFF" />
于 2012-10-02T10:43:01.447 回答