java - OnClick 后活动关闭
问题描述
所以我正在使用一个 Viewpager,其中有 3 个片段。制作了一个片段用于发送消息,但我遇到了问题,因为它在我单击按钮发送消息后立即关闭。
消息已发送并且数据库已更新,但片段每次都关闭并将我发送到我的视图页面中的第一个片段。
所以我接下来要做的是从我的 Viewpager 中删除 Message Fragment 并放置一个按钮,将我带到一个活动,其全部目的是发送消息。
问题是,即使一切正常,活动也会在发送消息后关闭,并将我带到具有 viewpager 的活动。
我从我的 Viewpager 中删除了该片段并尝试将其放入活动中以查看问题是否与片段有关,因为我从未真正使用过片段。
我的“聊天活动”
@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。
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 更改为 RecyclerView ,它没有为我做任何事情。我有另一个具有相同确切方法的活动,但这不会发生,无论如何这就是答案。
推荐阅读
- python - python 在子图中绘制不同的轴标题(边际)
- c++ - 在C语言编写的用户主函数进入之前做了什么?
- javascript - 如何使用可编辑的 HTML 页面和不同级别的用户添加元素来更新 javascript
- c - 我正在尝试将来自用户的输入字符与数组结构的第一个字母进行比较
- python - Python - 将 xml.etree.cElementTree 作为 ET 导入的 Xml 注释
- javascript - 全局变量更改时如何设置组件的状态?
- julia - 如何在 Julia 中将二维数组转换为一维数组
- typescript - 尝试在 VSCode 上调试 TypeScript 程序时出现“语法错误:无法在模块外使用 import 语句”
- javascript - 如何使用新数据更新 chart.js 图表?
- sql - SQL - 如何从查询结果中的 WHERE IN 子句中选择不匹配的参数