Subhaprasad Mukherjee Subhaprasad Mukherjee - 22 days ago 7
Android Question

App keeps crashing while checking for user sign in Firebase

package com.example.demo.myblog;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

private RecyclerView mReclBlogMainList; //The main recycler view for listing posts
private DatabaseReference mDatabaseRef;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAuth = FirebaseAuth.getInstance();
mAuthStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null){
Toast.makeText(MainActivity.this, "User null", Toast.LENGTH_SHORT).show();
// startActivity(new Intent(MainActivity.this, LoginActivity.class));
// finish(); //can't go back to previous activity # MainActivity
}
}
};

//All our posts reside inside the Blog directory under the database root
mDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Blog");

//Initialize Recycler View
mReclBlogMainList = (RecyclerView)findViewById(R.id.reclBlogMainList);
mReclBlogMainList.setHasFixedSize(true);
//setting a layout manager for recycler view -- vertical list
mReclBlogMainList.setLayoutManager(new LinearLayoutManager(this));
}

@Override
protected void onStart() {
super.onStart();
//adding auth state listener
mAuth.addAuthStateListener(mAuthStateListener);
//Creating adapter after creating view holder class below
FirebaseRecyclerAdapter<Blog, BlogViewHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class,
mDatabaseRef
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
//setting title, desc, image to view holder after fetch from database
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage()); //since View holder is static class
}
};
//setting up recycler view with firebase adapter
mReclBlogMainList.setAdapter(firebaseRecyclerAdapter);
}

//inflate the menu created in the main_menu file
// onto the top menu bar of the app
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//called when user selects any icon on the inflated menu
if(item.getItemId() == R.id.action_add){
// if the user has clicked on the '+' icon
startActivity(new Intent(MainActivity.this, PostActivity.class));
}
return super.onOptionsItemSelected(item);
}

//For setting up a recycler view, we need a view holder
//Creating view holder after setting up Blog class
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);

mView = itemView;
}

//Set the title and desc for the recycler view posts
public void setTitle(String title){
TextView postTitle = (TextView)mView.findViewById(R.id.txtPostTitle);
postTitle.setText(title);
}
public void setDesc(String desc){
TextView postDesc = (TextView)mView.findViewById(R.id.txtPostDesc);
postDesc.setText(desc);
}
//as it's inside a static class
public void setImage(Context ctx, String imageUrl){
ImageView postImage = (ImageView)mView.findViewById(R.id.imgPostImage);

//with the application context, load the image from
//the file url into the image view
Picasso.with(ctx).load(imageUrl).into(postImage);
}
}
}


This is the main page of a simple blog app. I am trying to check for user sign in using the
AuthStateListener
in the
OnCreate
function. However, it never gets called when I launch the app.

Also, when I add the auth state listener to Firebase auth instance in the
OnStart
function, my app crashes on lauch.

What am I doing wrong? How do I check for user login?

I am using the Google sign in for Firebase.

I am getting the following error.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo.myblog/com.example.demo.myblog.LoginActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Answer Source

The problem is in this error line:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo.myblog/com.example.demo.myblog.LoginActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

You need to use Theme.AppCompat for the MainActivity theme in AndroidManifest.xml

You need to use the theme from res/style.xml like this:

<resources>

  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>

</resources>

Something like this:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"/>