我已经创建了一个自定义适配器,将图像放在我的listView中,但在textView.setText(text)中给了我一个空指针异常。
这是我的适配器异常的部分:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageFileView);
TextView textView = (TextView) rowView.findViewById(R.id.fileName);
int pos = position+4;
String name = Names[pos];
String extFile = Names[pos].substring(Names[pos].lastIndexOf(".")+1);
String uri = "drawable/"+extFile+".png";
textView.setText(name);
Uri imgUri=Uri.parse(uri);
imageView.setImageURI(imgUri);
return rowView;
}
当我有异常时,这是我的LogCat:
06-14 12:15:04.616: E/AndroidRuntime(678): FATAL EXCEPTION: main
06-14 12:15:04.616: E/AndroidRuntime(678): java.lang.NullPointerException
06-14 12:15:04.616: E/AndroidRuntime(678): at com.example.poc_cubbyhole.ItemAdapter.getView(ItemAdapter.java:59)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.ListView.makeAndAddView(ListView.java:1745)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.ListView.fillSpecific(ListView.java:1290)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.ListView.layoutChildren(ListView.java:1588)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.view.View.layout(View.java:7175)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.os.Looper.loop(Looper.java:123)
06-14 12:15:04.616: E/AndroidRuntime(678): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 12:15:04.616: E/AndroidRuntime(678): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 12:15:04.616: E/AndroidRuntime(678): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 12:15:04.616: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 12:15:04.616: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 12:15:04.616: E/AndroidRuntime(678): at dalvik.system.NativeStart.main(Native Method)
这是在我的listView中创建项目的xml文件:
<ImageView
android:id="@+id/imageFileView"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="9dp"
android:layout_alignParentTop="true"/>
<TextView
android:layout_alignBottom="@+id/fileName"
android:layout_width="97dp"
android:layout_height="32dp"
android:id="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_marginLeft="66dp"
android:layout_alignParentRight="true"
android:gravity="center_vertical"/>
这是我的listView:
<com.example.poc_cubbyhole.widgets.PullToRefreshListView
android:id="@+id/list_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button_upload"
android:layout_alignParentTop="true"
/>
我使用小部件更改添加拉动和刷新到我的listView
答案 0 :(得分:3)
更改此
TextView textView = (TextView) rowView.findViewById(R.id.fileName);
到
TextView textView = (TextView) rowView.findViewById(R.id.textView);
在您的getView
方法中,因为您的TextView
ID为textView
而不是fileName
答案 1 :(得分:0)
你可以从阵列上的位置得到空值:
int pos = position+4;
String name = Names[pos];
对于中级解决方案,您可以添加以下内容:
int pos = position+4;
String name = ""+Names[pos];
所以你没有空值而且没有得到nullpointer。
现在,如果你想要获得nul值的正确解决方案,那么每次执行时都要调试应用程序并检查数组和名称的价值。 祝你好运..
答案 2 :(得分:0)
即使我的View ID正确,我也遇到了问题。但最后我更改了id并清理了项目然后构建了一次。它工作正常。