在滚动后单击(垂直)我得到空指针异常而不是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;
}
On Activity(两个功能):
@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);
}
}
});
}
有人请吗?
EDIT1:
的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);
答案 0 :(得分:1)
我认为它不能获取xml文件的引用所以nullpointerexception
TextView mtext = (TextView)v.findViewById(R.id.grid_item_text);
答案 1 :(得分:1)
尝试更换行
TextView mtext = (TextView) parent.getChildAt(position).findViewById(R.id.grid_item_text);
by
TextView mtext = (TextView) v.findViewById(R.id.grid_item_text);
答案 2 :(得分:1)
OnItemClickListener
本身提供itemview
。当你使用gridView.Child
时,它只返回当前项目,但不是所有这些都是你遇到问题的原因......
使用:
v.findViewById(R.id.itemid);
答案 3 :(得分: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" />