ListView 最初添加到它的工作,但如果我回溯并尝试再次添加我得到一个错误
ListView initially adding to it works, but if I back track and try adding again I get a error
我是 android 编程的新手,在过去一天左右的时间里,我的代码中一直存在错误。
当我 运行 我的实时聊天时,我可以按预期发送和阅读消息,但是当我返回显示我的对话列表的页面并单击返回实时聊天并重新发送我的消息时应用程序崩溃并出现以下错误。这是为什么?我该如何解决?
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:282)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:213)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:207)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:193)
at com.example.chatbox4.adapterListLiveChat.<init>(adapterListLiveChat.java:36)
at com.example.chatbox4.LiveChatFragment.onChildAdded(LiveChatFragment.java:131)
相关代码
聊天片段
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AppCompatActivity activity=(AppCompatActivity)v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, new LiveChatFragment(groupId.get(position),groupName.get(position),name,schoolId)).addToBackStack(null).commit();
groupId.clear();
groupName.clear();
}
});
实时聊天片段
data = FirebaseDatabase.getInstance().getReference().child("ChatLogs").child(gid);
data.addChildEventListener(new ChildEventListener() {
@Override
//this runs first
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId); //SECOND ERROR ON LINE 131
list.setAdapter(customList);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId);
list.setAdapter(customList);
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return v;
}
private void showChat(DataSnapshot snapshot) {
Iterator i = snapshot.getChildren().iterator();
while (i.hasNext()){
liveMessage.add((String) ((DataSnapshot)i.next()).getValue());
ownerId.add((String) ((DataSnapshot)i.next()).getValue());
ownerName.add((String) ((DataSnapshot)i.next()).getValue());
thumbnailUrl.add((String) ((DataSnapshot)i.next()).getValue());
timePosted.add((Long) ((DataSnapshot)i.next()).getValue());
}
}
用于实时聊天的自定义适配器
public class adapterListLiveChat extends ArrayAdapter{
ArrayList<String> liveMessage = new ArrayList<String>();
ArrayList<String> ownerId = new ArrayList<String>();
ArrayList<String> ownerName = new ArrayList<String>();
ArrayList<String> thumbnailUrl = new ArrayList<String>();
ArrayList<Long> timePosted = new ArrayList<Long>();
Context context;
public adapterListLiveChat(Context context, ArrayList<String> liveMessage, ArrayList<String> ownerName, ArrayList<Long> timePosted, ArrayList<String> thumbnailUrl, ArrayList<String> ownerId) {
super(context,R.layout.livechatlayout, ownerName); //FIRST ERROR ON LINE 36
this.context = context;
this.liveMessage = liveMessage;
this.ownerId = ownerId;
this.ownerName = ownerName;
this.thumbnailUrl = thumbnailUrl;
this.timePosted = timePosted;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(convertView == null){
LayoutInflater layoutInflater = LayoutInflater.from(context);
row = layoutInflater.inflate(R.layout.livechatlayout, null);
TextView userName = (TextView) row.findViewById(R.id.userName);
TextView message = (TextView) row.findViewById(R.id.textView);
final ImageView profilePic = (ImageView) row.findViewById(R.id.profilePic);
如果片段尚未附加,则“getContext()”returns NULL。 Android Studio 应该已经警告过你了。
您必须从其他地方(从“onAttach(Context)”)或以不同的方式获取上下文。
我没有在每次 onchildAdded 和 onChildChanged 时都创建一个新的 customList 适配器,而是在 addChildEventListener 之前初始化它。
final adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId);
data.addChildEventListener(new ChildEventListener() {
@Override
//this runs first
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
list.setAdapter(customList);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
list.setAdapter(customList);
}
我是 android 编程的新手,在过去一天左右的时间里,我的代码中一直存在错误。
当我 运行 我的实时聊天时,我可以按预期发送和阅读消息,但是当我返回显示我的对话列表的页面并单击返回实时聊天并重新发送我的消息时应用程序崩溃并出现以下错误。这是为什么?我该如何解决?
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:282)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:213)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:207)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:193)
at com.example.chatbox4.adapterListLiveChat.<init>(adapterListLiveChat.java:36)
at com.example.chatbox4.LiveChatFragment.onChildAdded(LiveChatFragment.java:131)
相关代码
聊天片段
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AppCompatActivity activity=(AppCompatActivity)v.getContext();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.container, new LiveChatFragment(groupId.get(position),groupName.get(position),name,schoolId)).addToBackStack(null).commit();
groupId.clear();
groupName.clear();
}
});
实时聊天片段
data = FirebaseDatabase.getInstance().getReference().child("ChatLogs").child(gid);
data.addChildEventListener(new ChildEventListener() {
@Override
//this runs first
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId); //SECOND ERROR ON LINE 131
list.setAdapter(customList);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId);
list.setAdapter(customList);
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return v;
}
private void showChat(DataSnapshot snapshot) {
Iterator i = snapshot.getChildren().iterator();
while (i.hasNext()){
liveMessage.add((String) ((DataSnapshot)i.next()).getValue());
ownerId.add((String) ((DataSnapshot)i.next()).getValue());
ownerName.add((String) ((DataSnapshot)i.next()).getValue());
thumbnailUrl.add((String) ((DataSnapshot)i.next()).getValue());
timePosted.add((Long) ((DataSnapshot)i.next()).getValue());
}
}
用于实时聊天的自定义适配器
public class adapterListLiveChat extends ArrayAdapter{
ArrayList<String> liveMessage = new ArrayList<String>();
ArrayList<String> ownerId = new ArrayList<String>();
ArrayList<String> ownerName = new ArrayList<String>();
ArrayList<String> thumbnailUrl = new ArrayList<String>();
ArrayList<Long> timePosted = new ArrayList<Long>();
Context context;
public adapterListLiveChat(Context context, ArrayList<String> liveMessage, ArrayList<String> ownerName, ArrayList<Long> timePosted, ArrayList<String> thumbnailUrl, ArrayList<String> ownerId) {
super(context,R.layout.livechatlayout, ownerName); //FIRST ERROR ON LINE 36
this.context = context;
this.liveMessage = liveMessage;
this.ownerId = ownerId;
this.ownerName = ownerName;
this.thumbnailUrl = thumbnailUrl;
this.timePosted = timePosted;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(convertView == null){
LayoutInflater layoutInflater = LayoutInflater.from(context);
row = layoutInflater.inflate(R.layout.livechatlayout, null);
TextView userName = (TextView) row.findViewById(R.id.userName);
TextView message = (TextView) row.findViewById(R.id.textView);
final ImageView profilePic = (ImageView) row.findViewById(R.id.profilePic);
如果片段尚未附加,则“getContext()”returns NULL。 Android Studio 应该已经警告过你了。
您必须从其他地方(从“onAttach(Context)”)或以不同的方式获取上下文。
我没有在每次 onchildAdded 和 onChildChanged 时都创建一个新的 customList 适配器,而是在 addChildEventListener 之前初始化它。
final adapterListLiveChat customList = new adapterListLiveChat(getContext(), liveMessage, ownerName, timePosted,thumbnailUrl,ownerId);
data.addChildEventListener(new ChildEventListener() {
@Override
//this runs first
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
list.setAdapter(customList);
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
showChat(snapshot);
list.setAdapter(customList);
}