Reputation: 143
My app continuously says "Unfortunately stopped". So I went through single line and found that the error is in below line
setSupportActionBar(toolbar);
I have searched here for some related post about this and they say to add this line
import androidx.appcompat.widget.Toolbar;
But I have added it also. Here is my code
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);
}
}
Upvotes: 0
Views: 93
Reputation: 15423
It's hard to say the actual problem without stack trace but it seems like
IllegalStateException: This Activity already has an action bar
If so then you have to use NoActionBar
variant of theme for the Activity / Application
Theme.AppCompat.Light.NoActionBar
Check official documentation about that.
Upvotes: 2