Firebase:RecyclerView:未连接适配器;跳过布局 [英] Firebase : RecyclerView: No adapter attached; skipping layout

查看:58
本文介绍了Firebase:RecyclerView:未连接适配器;跳过布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Firebase,到目前为止它很棒.但是,当我尝试使用Firebase UI和Firebase RecyclerAdapter将数据挂接到卡片视图中时,在日志猫上出现以下错误,提示

I just started out with Firebase and its been great so far. However, when I try to hook my data in a card view using Firebase UI and Firebase RecyclerAdapter, I get the following error on my log cat which says

    $ E/RecyclerView: No adapter attached; skipping layout

这是我的MainActivity.java

This is my MainActivity.java

package helloworld.firebase.com.helloworld;

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.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    TextView text1, text2 ;
    EditText edittitle, editdesc;
    Button btn, post;
    ListView mUserList;

   private RecyclerView mRecyclerView;
    private SignInButton mGooglebtn;
    private static final int RC_SIGN_IN =  1;
    private GoogleApiClient mGoogleApiClient;
    private DatabaseReference mDatabase;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListner;
    private FirebaseRecyclerAdapter firebaseRecyclerAdapter;
    private RecyclerView.LayoutManager mLayoutManager;



    private static final String TAG = MainActivity.class.getSimpleName();

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



        mRecyclerView = (RecyclerView) findViewById(R.id.blog_list);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(firebaseRecyclerAdapter);






        mGooglebtn =   (SignInButton) findViewById(R.id.googleBtn);
        edittitle = (EditText) findViewById(R.id.edit_title);
        editdesc = (EditText) findViewById(R.id.edit_desc);
        post = (Button) findViewById(R.id.post);

        post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startPosting();
            }
        });


        mAuth = FirebaseAuth.getInstance();
        mAuthListner = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null) {
                    Toast.makeText(MainActivity.this, "LOGGED IN", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, AccountActivity.class));
                }
            }
        };
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");

        final String userID = mDatabase.push().getKey();


        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                        Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
                    }
                })
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        mGooglebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });



    private void startPosting() {
        String name = edittitle.getText().toString().trim();
        String desc = editdesc.getText().toString().trim();

        DatabaseReference newPost = mDatabase.child("Blog").push();
        newPost.child("title").setValue(name);
        newPost.child("desc").setValue(desc);

    }


    protected void OnStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListner);

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, BlogViewHolder>(User.class,
                R.layout.blog_row,
                BlogViewHolder.class,
                mDatabase) {

            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, User model, int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDesc());
            }



        };

        mRecyclerView.setAdapter(firebaseRecyclerAdapter);

    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public BlogViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }

        public void setTitle(String title) {

            TextView posttitle = (TextView) mView.findViewById(R.id.post_title);
            posttitle.setText(title);
        }

        public void setDesc (String desc) {
            TextView postdesc = (TextView) mView.findViewById(R.id.post_desc);
            postdesc.setText(desc);
        }


    }




    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                // Google Sign In failed, update UI appropriately
                // ...
                Toast.makeText(this, "FAILED", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
        Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());

        AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                        Toast.makeText(MainActivity.this, "LOGGED IN", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(MainActivity.this, AccountActivity.class));
                        // If sign in fails, display a message to the user. If sign in succeeds
                        // the auth state listener will be notified and logic to handle the
                        // signed in user can be handled in the listener.
                        if (!task.isSuccessful()) {
                            Log.w(TAG, "signInWithCredential", task.getException());
                            Toast.makeText(MainActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                        // ...
                    }
                });
    }


}
        //});

这是我的User.class文件

And this is my User.class file

    package helloworld.firebase.com.helloworld;



public class User {

    public String title, desc;

    public User() {}

    public User(String title, String desc) {
        this.title = title;
        this.desc = desc;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

该应用程序运行正常,但未连接RecyclerView适配器.这是我现在面临的唯一问题.如果有人可以提供帮助,那就太好了.在此先感谢:)

The app runs perfectly but the RecyclerView adapter isn't getting attached. That is the only issue that I am facing right now. Would be great if someone can help out. Thanks in advance :)

推荐答案

基于android生命周期回调,onStart()仅在onCreate()之后被调用.并且在代码中,您尝试在初始化之前将firebaseRecyclerAdapter附加到适配器.

Based on the android life cycle callbacks, onStart() gets called only after onCreate(). And in your code, you tried to attach firebaseRecyclerAdapter to adapter before it was initialised.

如果您希望摆脱该烦人的消息,请在onCreate()内设置空适配器,或将所有FirebaseRecyclerAdapter代码移至onCreate,然后设置适配器

If you wish to get rid from that annoying message, either you set empty adapter inside onCreate() or move all of your FirebaseRecyclerAdapter codes to onCreate and then set an adapter .

这篇关于Firebase:RecyclerView:未连接适配器;跳过布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆