2017-05-09 40 views
0

我剛開始用火力地堡和是偉大至今。然而,當我試圖用鉤UI火力地堡火力地堡和我RecyclerAdapter在卡片視圖的數據,我得到我的日誌貓以下錯誤,說Firebase:RecyclerView:無連接適配器;跳繩佈局

$ E/RecyclerView: No adapter attached; skipping layout 

這是我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文件

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適配器沒有越來越重視。這是我現在面臨的唯一問題。如果有人能幫忙,會很棒。基於Android的生命週期回調提前:)

+0

請加firebaserecycleradapter類也 – TUSHAR

+0

我創建在MainActivity定製FirebaseListAdapter子類來代替。 下的OnStart()函數。 – Sivan

回答

0

感謝,onStart()只得到onCreate()後調用。在你的代碼中,你試圖在初始化之前將firebaseRecyclerAdapter附加到適配器上。

如果您希望擺脫那個令人討厭的消息,您可以在onCreate()內設置空適配器,或者將所有FirebaseRecyclerAdapter代碼移動到onCreate,然後設置適配器 。

+0

嘿三江源這麼多:) 就像你說的,我把整個FirebaseRecyclerAdapter代碼的OnCreate和它完美的作品:) – Sivan

0

檢查解決課題here

你需要,當你初始化你RecyclerView附加適配器和連接佈局管理等

相關問題