gpt4 book ai didi

java - 选择 TextView 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:16:52 25 4
gpt4 key购买 nike

我在选择 TextView 时遇到一些奇怪的问题。所以基本上我在顶部有一个 ListView ,在底部有一个文本框和按钮。 ListView 中填充了从数据库中完美检索的数据,没有错误。但是,当我尝试在 TextView 中输入内容时,我收到一些错误消息:

11-15 19:14:28.622: D/dalvikvm(1686): GC_CONCURRENT freed 622K, 13% free 10976K/12551K, paused 2ms+3ms
11-15 19:14:32.747: D/dalvikvm(1686): GC_CONCURRENT freed 431K, 12% free 11080K/12551K, paused 107ms+3ms
11-15 19:14:35.059: D/dalvikvm(1686): GC_CONCURRENT freed 428K, 11% free 11283K/12615K, paused 29ms+6ms
11-15 19:14:35.075: W/SurfaceView(1686): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false
11-15 19:14:42.973: D/dalvikvm(1686): GC_CONCURRENT freed 712K, 14% free 11216K/12935K, paused 2ms+3ms
11-15 19:15:27.770: D/AndroidRuntime(1686): Shutting down VM
11-15 19:15:27.770: W/dalvikvm(1686): threadid=1: thread exiting with uncaught exception (group=0x40c7e1f8)
11-15 19:15:27.864: E/AndroidRuntime(1686): FATAL EXCEPTION: main
11-15 19:15:27.864: E/AndroidRuntime(1686): java.lang.NullPointerException
11-15 19:15:27.864: E/AndroidRuntime(1686): at nyp.edu.eneighbourhood.EventChat$ListAdapter.getView(EventChat.java:112)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.AbsListView.obtainView(AbsListView.java:2201)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.ListView.makeAndAddView(ListView.java:1774)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.ListViewa.fillSpecific(ListView.java:1318)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.ListView.layoutChildren(ListView.java:1603)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.AbsListView.onLayout(AbsListView.java:2052)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1634)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1492)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.LinearLayout.onLayout(LinearLayout.java:1405)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.View.layout(View.java:11447)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewGroup.layout(ViewGroup.java:4224)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1671)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.os.Looper.loop(Looper.java:137)
11-15 19:15:27.864: E/AndroidRuntime(1686): at android.app.ActivityThread.main(ActivityThread.java:4512)
11-15 19:15:27.864: E/AndroidRuntime(1686): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 19:15:27.864: E/AndroidRuntime(1686): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 19:15:27.864: E/AndroidRuntime(1686): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
11-15 19:15:27.864: E/AndroidRuntime(1686): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
11-15 19:15:27.864: E/AndroidRuntime(1686): at dalvik.system.NativeStart.main(Native Method)
11-15 19:15:40.668: I/Process(1686): Sending signal. PID: 1686 SIG: 9

下面是我填充 ListView 以及单击提交按钮的代码:

public class EventChat extends Fragment {
Context context;
View eventChat;

String userID, eventID;

private ListView listview;
public ArrayList<EventComment> _commentlist = new ArrayList<EventComment>();
TextView txtDisplayCommentBy, txtDisplayDateTime, txtDisplayCommentDesc,
txtEventChat;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

eventChat = inflater.inflate(R.layout.event_chat, container, false);
context = getActivity();

listview = (ListView) eventChat.findViewById(R.id.listview);
txtEventChat = (TextView) eventChat.findViewById(R.id.txtEventChat);

Intent i = getActivity().getIntent();
_commentlist = (ArrayList<EventComment>) i
.getSerializableExtra("eventCommentObj");
Event eventModel = (Event) i.getSerializableExtra("eventObj");

userID = "Gab";
eventID = eventModel.getEventID();

listview.setAdapter(new ListAdapter(getActivity()));

return eventChat;
}

public void onSubmitChatClicked(View view){
Log.i("C","C");
EventComment commentModel = new EventComment();
String currentDate = EventDateTime.getCurrentDate();
String currentTime = EventDateTime.getCurrentTime();
String commentDesc = String.valueOf(txtEventChat.getText());

commentModel.setCommentBy(userID);
commentModel.setEventID(eventID);
commentModel.setCommentDate(currentDate);
commentModel.setCommentTime(currentTime);
commentModel.setCommentDesc(commentDesc);
new CreateCommentAsyncTask(context).execute(commentModel);

}

private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;

public ListAdapter(Context context) {
inflater = LayoutInflater.from(context);
}

public int getCount() {
return _commentlist.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {

convertView = inflater.inflate(R.layout.eventchat_listview_row,
null);
viewHolder = new ViewHolder();

viewHolder.txt_dcommentBy = (TextView) convertView
.findViewById(R.id.txtDisplayCommentBy);

viewHolder.txt_ddateTime = (TextView) convertView
.findViewById(R.id.txtDisplayDateTime);

viewHolder.txt_dcommentDesc = (TextView) convertView
.findViewById(R.id.txtDisplayCommentDesc);

} else {
viewHolder = (ViewHolder) convertView.getTag();
}

viewHolder.txt_dcommentBy.setText(_commentlist.get(position)
.getCommentBy().trim());

viewHolder.txt_ddateTime
.setText(_commentlist.get(position).getCommentDate().trim()
+ " "
+ _commentlist.get(position).getCommentTime()
.trim());

viewHolder.txt_dcommentDesc.setText(_commentlist.get(position)
.getCommentDesc().trim());

return convertView;
}
}

private class ViewHolder {
TextView txt_dcommentBy;
TextView txt_ddateTime;
TextView txt_dcommentDesc;
}
}

如果我只查看 ListView 而不在文本框中输入任何内容,则工作正常。仅当我在文本框中选择后才会出现该错误。有什么想法吗?

提前致谢。

最佳答案

第一次设置它时一切都很好,因为 convertView == null 并且您只需创建一个新的。但是,当您的 convertView != null 时,您会获取它的标签并将其转换为 ViewHolder

到那时它可能为空,因为您从未实际设置过标签

而是将其添加到第一个 if block 中:

convertView.setTag(viewHolder);

此外,ViewHolder 模式通常是自定义适配器的静态内部类:

private class ListAdapter extends BaseAdapter {
private static class ViewHolder {
TextView txt_dcommentBy;
TextView txt_ddateTime;
TextView txt_dcommentDesc;
}
...
}

关于java - 选择 TextView 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26945247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com