我的应用程序不断说“不幸停止”。所以我浏览了单行,发现错误在下面的行
setSupportActionBar(toolbar);
我在这里搜索了一些与此相关的帖子,他们说要添加此行
import androidx.appcompat.widget.Toolbar;
但是我也添加了它。 这是我的代码
package com.locationtracker2019;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
public class ListOnline extends AppCompatActivity {
//Firebase
DatabaseReference onlineRef, currentUserRef, counterRef;
FirebaseRecyclerAdapter<User, ListOnlineViewHolder> adapter;
//View
RecyclerView listOnline;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_online);
//Init View
listOnline = (RecyclerView)findViewById(R.id.listOnline);
listOnline.setHasFixedSize(true);
layoutManager = new LinearLayoutManager (this);
listOnline.setLayoutManager(layoutManager);
//set Toolbar and Logout / Join menu
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
toolbar.setTitle("LocationTracker2019"); //up to here everything is fine
setSupportActionBar(toolbar);
//Firebase
onlineRef = FirebaseDatabase.getInstance().getReference().child(".info/connected");
counterRef = FirebaseDatabase.getInstance().getReference("lastOnline"); //Create new child name lastOnline
currentUserRef = FirebaseDatabase.getInstance().getReference("lastOnline")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()); //Create new child in lastOnline with key is uid
setUpSystem();
//after setup system, just load all user from counterRef and display on RecyclerView
//this is online list
updateList();
}
private void updateList() {
adapter = new FirebaseRecyclerAdapter<User, ListOnlineViewHolder>(
User.class,
R.layout.user_layout,
ListOnlineViewHolder.class,
counterRef
) {
@Override
protected void populateViewHolder(ListOnlineViewHolder listOnlineViewHolder, User user, int i) {
listOnlineViewHolder.txtEmail.setText(user.getEmail());
}
};
adapter.notifyDataSetChanged();
listOnline.setAdapter(adapter);
}
private void setUpSystem() {
onlineRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue(Boolean.class)){
currentUserRef.onDisconnect().removeValue();
//set online user in list
counterRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(new User(FirebaseAuth.getInstance().getCurrentUser().getEmail(), "Online"));
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
counterRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
User user = postSnapshot.getValue(User.class);
Log.d("LOG",""+user.getEmail()+" is "+user.getStatus());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.action_join:
counterRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(new User(FirebaseAuth.getInstance().getCurrentUser().getEmail(), "Online"));
break;
case R.id.action_logout:
currentUserRef.removeValue();
break;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:2)
很难说没有堆栈跟踪的实际问题,但是看起来
IllegalStateException:此活动已经有一个操作栏
如果是这样,则必须为NoActionBar
使用主题的Activity / Application
变体
Theme.AppCompat.Light.NoActionBar