public class c_Message_Activity extends AppCompatActivity {
CircleImageView s_profile_image;
TextView s_profile_name;
ImageView c_btn_chat_send;
EditText c_txt_send;
FirebaseUser fuser;
DatabaseReference reference;
Intent intent;
c_MessageAdapter messageAdapter;
List<c_Chat> mchat;
RecyclerView recyclerView;
ValueEventListener seenListener;
AlertDialog.Builder builder;
private Context mContext;
public c_Message_Activity(Context mContext) {
this.mContext = mContext;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c_message_activity);
Toolbar toolbar = findViewById(R.id.c_chat_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(c_Message_Activity.this, c_MainActivty.class);
startActivity(intent);
finish();
}
});
recyclerView = findViewById(R.id.c_chat_recycler_view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
//Show user name
s_profile_image = findViewById(R.id.s_profile_image);
s_profile_name = findViewById(R.id.s_profile_name);
intent = getIntent();
final String userid = intent.getStringExtra("userid");
fuser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("User").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
s_profile_name.setText(user.getName());
if(user.getImage() != null && user.getImage().equals("default")){
s_profile_image.setImageResource(R.mipmap.ic_launcher);
}else {
Glide.with(getApplicationContext()).load(user.getImage()).into(s_profile_image);
}
readMessages(fuser.getUid(), userid, user.getImage());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
seenMessage(userid);
//Send Function
c_btn_chat_send = findViewById(R.id.c_btn_chat_send);
c_txt_send = findViewById(R.id.c_txt_send);
c_btn_chat_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = c_txt_send.getText().toString();
if(!msg.equals("")){
sendMessage(fuser.getUid(), userid, msg);
}else{
Toast.makeText(c_Message_Activity.this, "You can't send empty message. Please try again~", Toast.LENGTH_SHORT).show();
}
c_txt_send.setText("");
}
});
}
}
private void sendMessage(final String sender, final String receiver, String message){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
if(message.equals("close file") || message.equals("CLOSE FILE")){
Toast.makeText(getApplicationContext(),"Generate Report Activated.",Toast.LENGTH_SHORT).show();
builder = new AlertDialog.Builder(c_Message_Activity.this, R.style.MyDialogTheme);
builder.setTitle("Generate Report")
.setMessage("Are confirm to generate report?")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(mContext, c_Report_Activity.class);
intent.putExtra("userid",receiver);
mContext.startActivity(intent);
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}else {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", sender);
hashMap.put("receiver", receiver);
hashMap.put("message", message);
hashMap.put("isseen", false);
reference.child("Chats").push().setValue(hashMap);
final DatabaseReference chatRef = FirebaseDatabase.getInstance().getReference("Chatlist")
.child(fuser.getUid())
.child(receiver);
chatRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.exists()) {
chatRef.child("id").setValue(receiver);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
}
I am trying to pass the putExtra at extends AppCompatActivity and I write the Intent intent = new Intent(mContext, c_Report_Activity.class); and it get null from the receiver.
Anyone can teach me how to pass the value to putExtra? Thank you very much
Unable to instantiate activity ComponentInfo{com.example.counselfyp2019/com.example.counselfyp2019.c_Message_Activity}: java.lang.InstantiationException: java.lang.Class has no zero argument constructor