我在从联系人列表页面打开联系人时遇到问题,它工作正常,但当我触摸用户打开消息活动时,它应该开始在那里聊天。
这是我的消息活动:
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);
}
});
这是我运行应用程序时的日志:
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);
}
});