我正在使用Firebase构建一个简单的博客应用。现在,当我从登录页面登录时,我输入包含工具栏和Recycler View的主要活动。现在,当我进入此活动时,应用程序每次都会崩溃 在FirebaseRecyclerAdapter中显示错误。
以下是代码:
package com.example.adi.blogger;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
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.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 com.squareup.picasso.Callback;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import static android.R.attr.checkBoxPreferenceStyle;
import static android.R.attr.description;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.L;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static android.transition.Fade.IN;
public class MainActivity extends AppCompatActivity {
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mListener;
RecyclerView cardRecyclerView;
DatabaseReference mainActivityDatabaseReference;
DatabaseReference mDatabaseUser;
String LOG_TAG ="BOO";
Toolbar myToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainActivityDatabaseReference = FirebaseDatabase.getInstance().getReference().child("BLOG_DATA");
mainActivityDatabaseReference.keepSynced(true);
Log.e(LOG_TAG,"YOU ARE JUST UNDER KEP SYNC METHOD");
myToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
cardRecyclerView = (RecyclerView)findViewById(R.id.blogRecyclerView);
cardRecyclerView.setLayoutManager(new LinearLayoutManager(this));
Log.e(LOG_TAG,"YOU ARE UNDER SETLAYOUT MANAGER IN ONCREATE");
mDatabaseUser = FirebaseDatabase.getInstance().getReference().child("user");
mAuth = FirebaseAuth.getInstance();
//mainActivityDatabaseReference = FirebaseDatabase.getInstance().getReference().child("BLOG_DATA");
mListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mUser = firebaseAuth.getCurrentUser();
Log.e(LOG_TAG,"YOU ARE DEFINITELY ENTERING AUTH STATE LISTENER");
if(mUser==null){
Intent getBackIntent = new Intent(MainActivity.this,LoginActivity.class);
getBackIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(getBackIntent);
}
Log.e(LOG_TAG,"YOU ARE COMING OUT OF IT NOW");
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.e(LOG_TAG,"you are in on create option menu");
getMenuInflater().inflate(R.menu.menu_xml,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.addButtonToolbar:
Intent newBlogIntent = new Intent(this,AddBlog.class);
startActivity(newBlogIntent);
break;
case R.id.signoutButtonToolbar:
mAuth.signOut();
Intent signOut_intent = new Intent(MainActivity.this,LoginActivity.class);
signOut_intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(signOut_intent);
break;
}
return true;
}
@Override
protected void onStart() {
Log.e(LOG_TAG,"You are now in onstart method");
Toast.makeText(this,"Making onstart",Toast.LENGTH_SHORT).show();
super.onStart();
mAuth.addAuthStateListener(mListener);
checkUserExist();
Log.e(LOG_TAG,"You are before creating firebase adapter");
FirebaseRecyclerAdapter<BlogGS,recyclerBlogView> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<BlogGS, recyclerBlogView>(
BlogGS.class,
R.layout.blog_row,
recyclerBlogView.class,
mainActivityDatabaseReference) {
@Override
protected void populateViewHolder(recyclerBlogView viewHolder, BlogGS model, int position) {
Log.e(LOG_TAG,"IN POPULATE VIEW");
Toast.makeText(getApplicationContext(),"You are entering populate View Holder method",Toast.LENGTH_SHORT).show();
viewHolder.setTitle(model.getTitle());
viewHolder.setDescription(model.getDescription());
viewHolder.setImageurl(getApplicationContext(),model.getImageurl());
}
};
cardRecyclerView.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.notifyDataSetChanged();
}
public class recyclerBlogView extends RecyclerView.ViewHolder {
View mView;
recyclerBlogView(View itemView) {
super(itemView);
Log.e(LOG_TAG,"you are in recyclerBlogView Class");
mView = itemView;
}
public void setTitle(String title){
TextView recylerTitle = (TextView)mView.findViewById(R.id.cardTitle);
recylerTitle.setText(title);
}
public void setDescription(String description){
TextView recyclerConten = (TextView)mView.findViewById(R.id.cardDescription);
recyclerConten.setText(description);
}
public void setImageurl(final Context ctx, final String imageurl){
final ImageView blogImage = (ImageView)findViewById(R.id.blogImageCard);
Picasso.with(ctx).load(imageurl).networkPolicy(NetworkPolicy.OFFLINE).into(blogImage, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(ctx).load(imageurl).centerCrop().into(blogImage);
}
});
}
}
private void checkUserExist() {
System.out.print("CHECK USER ENTERED");
Toast.makeText(getApplicationContext(),"you are entering check user exist method",Toast.LENGTH_SHORT).show();
Log.e(LOG_TAG,"check user exist method");
final String user_id = mAuth.getCurrentUser().getUid();
mDatabaseUser.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(!dataSnapshot.hasChild(user_id)){
Intent accountSetupIntent = new Intent(MainActivity.this,AccountSetup.class);
accountSetupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(accountSetupIntent);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
以下是LogCat说明
04-07 01:48:53.439 7820-7820/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.adi.blogger, PID: 7820
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:172)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6290)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5478)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2969)
at android.view.View.measure(View.java:18811)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18811)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5952)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18811)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2127)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1243)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1479)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6045)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
at android.view.Choreographer.doCallbacks(Choreographer.java:672)
at android.view.Choreographer.doFrame(Choreographer.java:608)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.NoSuchMethodException: <init> [class android.view.View]
at java.lang.Class.getConstructor(Class.java:528)
at java.lang.Class.getConstruct
04-07 01:48:53.469 7820-7820/? I/Process: Sending signal. PID: 7820 SIG: 9
答案 0 :(得分:0)
由于您已将recyclerBlogView
放入另一个班级,因此需要将其标记为static
。所以:
public static class recyclerBlogView extends RecyclerView.ViewHolder {
另见Github问题:https://github.com/firebase/FirebaseUI-Android/issues/210