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);
}
}
}
这是一个简单的博客应用程序的主页面。我正在尝试使用AuthStateListener
功能中的OnCreate
来检查用户是否已登录。但是,当我启动应用程序时,它永远不会被调用。
此外,当我在OnStart
函数中将auth状态侦听器添加到Firebase auth实例时,我的应用程序崩溃了。
我做错了什么?如何检查用户登录?
我正在使用Google登录Firebase。
我收到以下错误。
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.demo.myblog / com.example.demo.myblog.LoginActivity}:java.lang.IllegalStateException:您需要使用Theme.AppCompat主题(这个活动的后代。
答案 0 :(得分:0)
请在onCreate方法中初始化FirebaseUser的对象,如下所示 -
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
并在您的Activity的onStart方法中查看
if (user != null) {
// Your user is authenticated
// Do your tasks;
}
答案 1 :(得分:0)
问题在于此错误行:
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.
您需要将Theme.AppCompat用于MainActivity
AndroidManifest.xml
主题
您需要使用res/style.xml
中的主题,如下所示:
<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>
这样的事情:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme"/>