SetAdapter 与 My FriendsArrayAdapter 上的 NullPointerException
异常发生在“rowTxt.setText”行上,我一生都无法弄清楚原因。
这是我的代码
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if(rowView == null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(mResourceId, null);
}
Friend friend = mFriendsList.get(position);
TextView rowTxt = (TextView) rowView.findViewById(R.id.rowtext);
Log.i("name", friend.getmName());
rowTxt.setText(friend.getmName());
return rowView;
}
Logcat 输出:
04-24 04:07:14.093:DEBUG/AndroidRuntime(553):关闭虚拟机 04-24 04:07:14.093:警告/dalvikvm(553):threadid = 1:线程因未捕获的异常而退出(组= 0x4001d800) 04-24 04:07:14.152:错误/AndroidRuntime(553):致命异常:主要 04-24 04:07:14.152: 错误/AndroidRuntime(553): java.lang.NullPointerException 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 com.friendsapp.FriendsArrayAdapter.getView(FriendsArrayAdapter.java:46) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.AbsListView.obtainView(AbsListView.java:1315) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.ListView.makeAndAddView(ListView.java:1727) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.ListView.fillDown(ListView.java:652) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.ListView.fillFromTop(ListView.java:709) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.ListView.layoutChildren(ListView.java:1580) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.AbsListView.onLayout(AbsListView.java:1147) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 android.view.View.layout(View.java:7035) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 android.view.View.layout(View.java:7035) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.FrameLayout.onLayout(FrameLayout.java:333) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 android.view.View.layout(View.java:7035) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.LinearLayout.onLayout(LinearLayout.java:1042) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 android.view.View.layout(View.java:7035) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.widget.FrameLayout.onLayout(FrameLayout.java:333) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 android.view.View.layout(View.java:7035) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.view.ViewRoot.performTraversals(ViewRoot.java:1045) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 android.os.Looper.loop(Looper.java:123) 04-24 04:07:14.152:错误/AndroidRuntime(553):在android.app.ActivityThread.main(ActivityThread.java:4627) 04-24 04:07:14.152:错误/AndroidRuntime(553):在java.lang.reflect.Method.invokeNative(本机方法) 04-24 04:07:14.152:错误/AndroidRuntime(553):在java.lang.reflect.Method.invoke(Method.java:521) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 04-24 04:07:14.152: 错误/AndroidRuntime(553): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-24 04:07:14.152:错误/AndroidRuntime(553):在dalvik.system.NativeStart.main(本机方法) 04-24 04:07:14.182: WARN/ActivityManager(65): 强制完成活动 com.friendsapp/.FriendsActivity 04-24 04:07:14.973: WARN/ActivityManager(65): HistoryRecord{44e9c3c8 com.friendsapp/.FriendsActivity} 的活动暂停超时 04-24 04:07:17.822:DEBUG/dalvikvm(553):GC_FOR_MALLOC 在 113 毫秒内释放了 5842 个对象/457336 字节 04-24 04:07:18.732:信息/进程(553):发送信号。 PID:553 SIG:9 04-24 04:07:18.772:信息/WindowManager(65):胜利死亡:窗口{4503df58 AtchDlg:com.friendsapp/com.friendsapp.FriendsActivity暂停= false} 04-24 04:07:18.772:INFO/ActivityManager(65):进程 com.friendsapp (pid 553) 已死亡。 04-24 04:07:18.782:信息/WindowManager(65):胜利死亡:窗口{44f78f88 com.friendsapp/com.friendsapp.FriendsActivity 暂停= false} 04-24 04:07:26.814: WARN/ActivityManager(65): HistoryRecord{44e9c3c8 com.friendsapp/.FriendsActivity} 的活动销毁超时 04-24 04:12:07.102:DEBUG / SntpClient(65):请求时间失败:java.net.SocketException:协议不支持地址族
编辑:我在corlettk的帮助下解决了这个问题。简而言之,我像这样分配我的列表适配器:
FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, android.R.simple_list_item_1, mFriendsList);
我这样做是因为最初我有一个通用的列表适配器,它不适合我的朋友对象;从来没有想过要改变它。
因此,当 setAdapter 进入我的 FriendsArrayAdapter 时,它在 simple_list_item_1 的 id 中搜索 R.id.rowtext,但没有找到。修复方法是将上面的调用更改为
FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, R.layout.row_layout, mFriendsList);
(row_layout 是我的 XML 文件的名称)
The exception is occurring on the line "rowTxt.setText" and I can't, for the life of me, figure out why.
Here's my code
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if(rowView == null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(mResourceId, null);
}
Friend friend = mFriendsList.get(position);
TextView rowTxt = (TextView) rowView.findViewById(R.id.rowtext);
Log.i("name", friend.getmName());
rowTxt.setText(friend.getmName());
return rowView;
}
Logcat output:
04-24 04:07:14.093: DEBUG/AndroidRuntime(553): Shutting down VM
04-24 04:07:14.093: WARN/dalvikvm(553): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): FATAL EXCEPTION: main
04-24 04:07:14.152: ERROR/AndroidRuntime(553): java.lang.NullPointerException
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at com.friendsapp.FriendsArrayAdapter.getView(FriendsArrayAdapter.java:46)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.makeAndAddView(ListView.java:1727)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.fillDown(ListView.java:652)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.fillFromTop(ListView.java:709)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.ListView.layoutChildren(ListView.java:1580)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.View.layout(View.java:7035)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.os.Looper.loop(Looper.java:123)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at java.lang.reflect.Method.invoke(Method.java:521)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 04:07:14.152: ERROR/AndroidRuntime(553): at dalvik.system.NativeStart.main(Native Method)
04-24 04:07:14.182: WARN/ActivityManager(65): Force finishing activity com.friendsapp/.FriendsActivity
04-24 04:07:14.973: WARN/ActivityManager(65): Activity pause timeout for HistoryRecord{44e9c3c8 com.friendsapp/.FriendsActivity}
04-24 04:07:17.822: DEBUG/dalvikvm(553): GC_FOR_MALLOC freed 5842 objects / 457336 bytes in 113ms
04-24 04:07:18.732: INFO/Process(553): Sending signal. PID: 553 SIG: 9
04-24 04:07:18.772: INFO/WindowManager(65): WIN DEATH: Window{4503df58 AtchDlg:com.friendsapp/com.friendsapp.FriendsActivity paused=false}
04-24 04:07:18.772: INFO/ActivityManager(65): Process com.friendsapp (pid 553) has died.
04-24 04:07:18.782: INFO/WindowManager(65): WIN DEATH: Window{44f78f88 com.friendsapp/com.friendsapp.FriendsActivity paused=false}
04-24 04:07:26.814: WARN/ActivityManager(65): Activity destroy timeout for HistoryRecord{44e9c3c8 com.friendsapp/.FriendsActivity}
04-24 04:12:07.102: DEBUG/SntpClient(65): request time failed: java.net.SocketException: Address family not supported by protocol
EDIT: I figured out the issue with the help of corlettk. In short, I was assigning my listadapter like so:
FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, android.R.simple_list_item_1, mFriendsList);
I did this because initially I had a generic listadapter that did not accommodate my Friend object; never thought to change it.
So when setAdapter went into my FriendsArrayAdapter it searched the id of simple_list_item_1 for R.id.rowtext, which it did't find. The fix was to change the above call to
FriendsArrayAdapter fAdapter = new FriendsArrayAdapter(FriendsActivity.this, R.layout.row_layout, mFriendsList);
(row_layout is the name of my XML file)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Fly,
Log.i("name",friend.getmName());
语句的输出是什么?如果 friend 不为空,那么我猜 rowTxt 是。调试打印它以找出...此外,我猜你已经得到了一个狡猾的视图ID;或者也许该视图尚未添加到集合中。您之前是否缺少一些初始化代码?
干杯。基思.
Fly,
What's the output of that
Log.i("name", friend.getmName());
statement? If friend is NOT null, then I'd guess that rowTxt is. Debug print it to find out...Further I'd guesse that you've got yourself a dodgy view-ID; or maybe that view just hasn't been added to a collection. Are you missing some initialisation code earlier on?
Cheers. Keith.