我正在尝试创建一个应用程序,使用户能够使用短信和音频消息进行交互。我能够发送和检索消息,但我遇到的问题是,当我尝试检查收到的消息是文本还是音频时,我得到NullPointerException。我想要做的是首先检查文本类型,然后根据消息类型选择适当的布局视图。我使用了适配器,这是我的代码:
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyMessageViewHolder>{
private ArrayList<Messages> arrayListMessages= new ArrayList<>();
private Context mcontext;
FirebaseAuth mAuth;
private String Type = null;
public static class myAudioMessageViewHolder extends RecyclerView.ViewHolder{
TextView rec_start;
TextView rec_end;
ImageButton myPlay;
SeekBar audio_seek;
public myAudioMessageViewHolder(View itemView){
super(itemView);
rec_start = itemView.findViewById(R.id.time_elapsed);
rec_end = itemView.findViewById(R.id.finish_time);
myPlay = itemView.findViewById(R.id.play_btn);
audio_seek = itemView.findViewById(R.id.my_seekbar);
}
}
public static class MyMessageViewHolder extends RecyclerView.ViewHolder{
TextView textViewMessage;
CircleImageView UserProfile;
public MyMessageViewHolder(View itemView) {
super(itemView);
textViewMessage = itemView.findViewById(R.id.messageTextView);
UserProfile = itemView.findViewById(R.id.message_profile_layout);
}
}
public ArrayAdapter adapter;
public MessageAdapter(Context context, ArrayList<Messages> arrayListMessages){
this.arrayListMessages = arrayListMessages;
mcontext = context;
}
@Override
public MyMessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType ) {
if (Type.equals("audio")){ // This should be the determinant on the view being chosen and it should call type from database
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_message_layout, parent, false);
return new MyMessageViewHolder(view);
}else if (Type.equals("text")){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent,false);
return new MyMessageViewHolder(view);
}
else {
return null;
}
}
@Override
public void onBindViewHolder(final MyMessageViewHolder holder, final int position) {
mAuth = FirebaseAuth.getInstance();
String mCurrentUser = mAuth.getCurrentUser().getUid();
final Messages messages = arrayListMessages.get(position);
String message_type = messages.getType();
String from_user = messages.getFrom();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
if(mCurrentUser.equals(messages.getFrom())){
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bg_primary);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.RIGHT);
holder.textViewMessage.setTextColor(Color.WHITE);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view)
{
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}else{
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bd_white);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.LEFT);
holder.textViewMessage.setTextColor(Color.BLACK);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
adapter.notifyDataSetChanged();
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}
holder.textViewMessage.setText(messages.getMessage());
if(message_type.equals("text")) {
Type = messages.getType(); // Here i am trying to get the message type and then use it to get Inflator view of a text type
holder.textViewMessage.setText(messages.getMessage());
}
else if(message_type.equals("audio"))
{
Type = messages.getType(); // Here i am trying to get the message type and then use it to get Inflator view of a audio type
}
}
@Override
public int getItemCount() {
return arrayListMessages.size();
}
}
这是我从以下地址获取消息的地方:
public class Messages {
String message,type,from;
long time;
boolean seen;
public Messages(){
// for datashnap shot
}
public Messages(String message, String type, long time, boolean seen, String from) {
this.message = message;
this.type = type;
this.time = time;
this.seen = seen;
this.from=from;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean isSeen() {
return seen;
}
public void setSeen(boolean seen) {
this.seen = seen;
}
}
在设备上运行应用程序后,出现以下错误:
FATAL EXCEPTION: main
Process: com.rescuex_za.rescuex, PID: 22285
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.rescuex_za.rescuex.MessageAdapter.onCreateViewHolder(MessageAdapter.java:92)
at com.rescuex_za.rescuex.MessageAdapter.onCreateViewHolder(MessageAdapter.java:37)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6519)
我正在检查并检查来自我的数据库的消息。我怎样才能阅读消息类型?请提供完整的详细信息或建议,因为我还是Android开发的新手,想要了解我在做什么。谢谢。
遵循AL建议的方法。我没有得到我正在寻找的观点这是我在问这个问题时发布的课程的更新。
1.Messages class
公共类消息{
public static final int TEXT_TYPE=0;
public static final int AUDIO_TYPE=1;
String message,type,from;
long time;
boolean seen;
int type_text;
public Messages(){
// for datashnap shot
}
public Messages(String message, String type, long time, boolean seen, String from) {
this.message = message;
this.type = type;
this.time = time;
this.seen = seen;
this.from=from;
if (type.equals("text")){
this.type_text=TEXT_TYPE;
} else if (type.equals("recording")){
this.type_text=AUDIO_TYPE;
}
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public boolean isSeen() {
return seen;
}
public void setSeen(boolean seen) {
this.seen = seen;
}
}
这是我的Adapter类
public class MessageAdapter extends
RecyclerView.Adapter<MessageAdapter.MyMessageViewHolder>{
private ArrayList<Messages> arrayListMessages= new ArrayList<>();
private Context mcontext;
FirebaseAuth mAuth;
private DatabaseReference mUserDatabase;
private ViewGroup parent;
public static class myAudioMessageViewHolder extends MyMessageViewHolder{
TextView rec_start;
TextView rec_end;
ImageButton myPlay;
SeekBar audio_seek;
public myAudioMessageViewHolder(View itemView){
super(itemView);
rec_start = itemView.findViewById(R.id.time_elapsed);
rec_end = itemView.findViewById(R.id.finish_time);
myPlay = itemView.findViewById(R.id.play_btn);
audio_seek = itemView.findViewById(R.id.my_seekbar);
}
}
public static class MyMessageViewHolder extends RecyclerView.ViewHolder{
TextView textViewMessage;
CircleImageView UserProfile;
public MyMessageViewHolder(View itemView) {
super(itemView);
textViewMessage = itemView.findViewById(R.id.messageTextView);
UserProfile = itemView.findViewById(R.id.message_profile_layout);
}
}
public ArrayAdapter adapter;
public MessageAdapter(Context context, ArrayList<Messages> arrayListMessages){
this.arrayListMessages = arrayListMessages;
mcontext = context;
}
@Override
public MyMessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType ) {
View view;
switch (viewType) {
case Messages.TEXT_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_single_layout, parent, false);
return new myAudioMessageViewHolder(view);
case Messages.AUDIO_TYPE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.audio_message_layout, parent, false);
return new MyMessageViewHolder(view);
}
return null;
}
@Override
public int getItemViewType(int position) {
switch (arrayListMessages.get(position).type_text) {
case 0:
return Messages.TEXT_TYPE;
case 1:
return Messages.AUDIO_TYPE;
default:
return -1;
}
}
@Override
public void onBindViewHolder(final MyMessageViewHolder holder, final int position) {
mAuth = FirebaseAuth.getInstance();
String mCurrentUser = mAuth.getCurrentUser().getUid();
final Messages messages = arrayListMessages.get(position);
String message_type = messages.getType();
String from_user = messages.getFrom();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
if(mCurrentUser.equals(messages.getFrom())){
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bg_primary);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.RIGHT);
holder.textViewMessage.setTextColor(Color.WHITE);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view)
{
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}else{
holder.textViewMessage.setBackgroundResource(R.drawable.custom_message_bd_white);
holder.textViewMessage.setTextIsSelectable(true);
holder.textViewMessage.setGravity(Gravity.LEFT);
holder.textViewMessage.setTextColor(Color.BLACK);
holder.textViewMessage.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
CharSequence options[] = new CharSequence[]{"Delete", "Copy Text"};
final AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Click Event for each item.
if (i == 0) {
String selectedItem = messages.getMessage();
arrayListMessages.remove(selectedItem);
adapter.notifyDataSetChanged();
Toast.makeText(mcontext, "Message deleted", Toast.LENGTH_SHORT).show();
}
if (i == 1)
{
ClipboardManager cm = (ClipboardManager) mcontext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(holder.textViewMessage.getText());
Toast.makeText(mcontext, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
return true;
}
});
}
holder.textViewMessage.setText(messages.getMessage());
if(message_type.equals("text")) {
// Here i am trying to get the message type and then use it to get Inflator view of a text type
holder.textViewMessage.setText(messages.getMessage());
}
else if(message_type.equals("recording"))
{
// Here i am trying to get the message type and then use it to get Inflator view of a audio type
}
}
@Override
public int getItemCount() {
return arrayListMessages.size();
}
}