gpt4 book ai didi

java - OnClick 后 Activity 关闭

转载 作者:行者123 更新时间:2023-12-02 09:53:19 26 4
gpt4 key购买 nike

所以我正在使用一个 Viewpager,其中有 3 个 fragment 。一个 fragment 是为了发送消息而制作的,但我遇到了一个问题,因为它在我单击发送消息的按钮后立即关闭。

消息已发送,数据库已更新,但 fragment 每次都会关闭,并将我发送到 View 页面中的第一个 fragment 。

接下来我要做的就是从 Viewpager 中删除消息 fragment ,并放置一个按钮,将我带到一个 Activity ,该 Activity 的全部目的是发送消息。

问题是,即使一切正常, Activity 也会在发送消息后关闭并将我带到具有viewpager的 Activity 。

我从 Viewpager 中删除了该 fragment ,并尝试将其放入 Activity 中以查看问题是否出在该 fragment 上,因为我从未真正使用过 fragment 。

我的“聊天 Activity ”

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
title = preferences.getString("Titulo", "Sin titulo");

setContentView(R.layout.activity_chat_grupo);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Chat");
mToolbar.setTitleTextColor(Color.WHITE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

mAuth = FirebaseAuth.getInstance();
mCurrent_user_id = mAuth.getCurrentUser().getDisplayName();
mPics = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrent_user_id).child("imagen");
messages = findViewById(R.id.list_of_messages);
input = (EditText) findViewById(R.id.input);

fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
SimpleDateFormat fecha = new SimpleDateFormat("dd-MM (HH:mm:ss)");
final String fecha_d = fecha.format(c.getTime());
mPics.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
url = dataSnapshot.getValue().toString();
Log.d("PHOTOURL", url);

FirebaseDatabase.getInstance().getReference().child("grupos").child(title).child("chat")
.push()
.setValue(new ChatGrupoAdapter(input.getText().toString(),
mCurrent_user_id, fecha_d, url)).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {

}
});
input.setText("");

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
});

adapter = new FirebaseListAdapter<ChatGrupoAdapter>(ChatGrupo.this, ChatGrupoAdapter.class,
R.layout.message, FirebaseDatabase.getInstance().getReference().child("grupos").child(title).child("chat")) {
@Override
protected void populateView(View v, ChatGrupoAdapter model, int position) {
TextView messageText = (TextView) v.findViewById(R.id.message_text);
TextView messageUser = (TextView) v.findViewById(R.id.message_user);
TextView messageTime = (TextView) v.findViewById(R.id.message_time);

CircleImageView messageUserpic = v.findViewById(R.id.user_image);
messageText.setText(model.getMensaje());
messageUser.setText(model.getUsuario());
Picasso.get().load(model.getUserphoto()).into(messageUserpic);
messageTime.setText(model.getMessageTime());

}
};
messages.setAdapter(adapter);

}

@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}

//logcat 在 ListView 中显示新消息后。

05-15 20:39:24.418 26610-26610/com.example.danny.trendy_reads D/Picasso: Main        created      [R51] Request{https://firebasestorage.googleapis.com/v0/b/trendyreads-658db.appspot.com/o/Imagenes%2Fvanessa.jpg%2Fcropped3415433131357908162.jpg?alt=media&token}
05-15 20:39:24.427 26610-26610/com.example.danny.trendy_reads D/Picasso: Main completed [R51] from MEMORY
05-15 20:39:24.429 26610-26610/com.example.danny.trendy_reads W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
endBatchEdit on inactive InputConnection
05-15 20:39:24.430 26610-26610/com.example.danny.trendy_reads W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
endBatchEdit on inactive InputConnection
05-15 20:39:24.483 26610-26610/com.example.danny.trendy_reads W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
05-15 20:39:24.540 26610-26610/com.example.danny.trendy_reads W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
05-15 20:39:24.546 26610-26610/com.example.danny.trendy_reads W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
05-15 20:39:24.572 26610-26610/com.example.danny.trendy_reads W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
endBatchEdit on inactive InputConnection
05-15 20:39:29.186 26610-26696/com.example.danny.trendy_reads V/FA: Inactivity, disconnecting from the service
05-15 20:39:46.519 26610-28425/com.example.danny.trendy_reads E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
05-15 20:39:46.519 26610-28425/com.example.danny.trendy_reads I/chatty: uid=10158(com.example.danny.trendy_reads) Binder:26610_5 identical 3 lines
05-15 20:39:46.519 26610-28425/com.example.danny.trendy_reads E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)

最佳答案

在尝试了所有方法之后,我发现添加“android:launchMode="singleInstance”解决了我的问题。(在 list 中)

我还将 List 更改为 RecyclerView,但它对我没有任何作用。我有另一个具有相同方法的 Activity ,但这种情况不会发生,无论如何,这就是答案。

关于java - OnClick 后 Activity 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com