[英]Having a problem when i touch a user to start chatting
我在從我的聯系人列表頁面打開聯系人時遇到問題,它工作正常,但是當我觸摸用戶打開消息活動時,它應該開始在那里聊天。
這是我的消息活動:
public class MessageActivity extends AppCompatActivity {
CircleImageView imageView;
FirebaseUser firebaseUser;
DatabaseReference reference;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
final Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
imageView = findViewById(R.id.profile_image);
intent = getIntent();
String userid = intent.getStringExtra("userid");
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("parent").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user.getImageURL().equals("default")) {
imageView.setImageResource(R.drawable.ic_user);
}else {
Glide.with(MessageActivity.this).load(user.getImageURL()).into(imageView);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
這就是我呼吁的意圖:
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MessageActivity.class);
intent.putExtra("userid", user.getId());
context.startActivity(intent);
}
});
這是我運行應用程序時的 logcat:
2020-05-01 20:39:12.857 12974-12974/net.gobz.gobz E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.gobz.gobz, PID: 12974
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:994)
at android.app.ContextImpl.startActivity(ContextImpl.java:970)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:389)
at net.gobz.gobz.Adapter.UserAdapter$1.onClick(UserAdapter.java:54)
閱讀您的 logcat 后,這表示您必須對 IntentActivity 使用 Flags,因此請嘗試用此代碼替換您的代碼。
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, MessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("userid", user.getId());
context.startActivity(intent);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.