2016-09-20 50 views
2

營造出NoSuchMethodException我知道,從https://github.com/firebase/FirebaseUI-Android/issues/46,修復NoSuchMethodException:FirebaseRecyclerAdapter如果使用密鑰庫版本

  1. 確保您Tas​​kViewHolder類是公共
  2. 如果您TaskViewHolder是一個內部類的如你的活動,確保它是靜態的

該解決方案僅在我使用調試密鑰庫構建應用程序時起作用。但是在使用release keystore構建應用程序之後,我仍然得到NoSuchMethodException。 minifyEnabled =假

測試Android上的一個棉花糖美圖A10,榮譽6和marsmallow模擬器

堆棧跟蹤錯誤:

Exception java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View] 
com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder (FirebaseRecyclerAdapter.java:172) 
android.support.v7.widget.RecyclerView$Adapter.createViewHolder (RecyclerView.java:5836) 
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5060) 
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:4970) 
android.support.v7.widget.LinearLayoutManager$LayoutState.next (LinearLayoutManager.java:2029) 
android.support.v7.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1414) 
android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1377) 
android.support.v7.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:578) 
android.support.v7.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:3315) 
android.support.v7.widget.RecyclerView.onMeasure (RecyclerView.java:2843) 
android.view.View.measure (View.java:18788) 
android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5951) 
android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1465) 
android.widget.LinearLayout.measureVertical (LinearLayout.java:748) 
android.widget.LinearLayout.onMeasure (LinearLayout.java:630) 
android.view.View.measure (View.java:18788) 
android.widget.ScrollView.measureChildWithMargins (ScrollView.java:1283) 
android.widget.FrameLayout.onMeasure (FrameLayout.java:194) 
android.widget.ScrollView.onMeasure (ScrollView.java:340) 
android.view.View.measure (View.java:18788) 
android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5951) 
android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1465) 
android.widget.LinearLayout.measureVertical (LinearLayout.java:748) 
android.widget.LinearLayout.onMeasure (LinearLayout.java:630) 
android.view.View.measure (View.java:18788) 
android.widget.RelativeLayout.measureChildHorizontal (RelativeLayout.java:715) 
android.widget.RelativeLayout.onMeasure (RelativeLayout.java:461) 
android.view.View.measure (View.java:18788) 
android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5951) 
android.widget.FrameLayout.onMeasure (FrameLayout.java:194) 
android.support.v7.widget.ContentFrameLayout.onMeasure (ContentFrameLayout.java:135) 
android.view.View.measure (View.java:18788) 
android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5951) 
android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1465) 
android.widget.LinearLayout.measureVertical (LinearLayout.java:748) 
android.widget.LinearLayout.onMeasure (LinearLayout.java:630) 
android.view.View.measure (View.java:18788) 
android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5951) 
android.widget.FrameLayout.onMeasure (FrameLayout.java:194) 
android.view.View.measure (View.java:18788) 
android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5951) 
android.widget.LinearLayout.measureChildBeforeLayout (LinearLayout.java:1465) 
android.widget.LinearLayout.measureVertical (LinearLayout.java:748) 
android.widget.LinearLayout.onMeasure (LinearLayout.java:630) 
android.view.View.measure (View.java:18788) 
android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5951) 
android.widget.FrameLayout.onMeasure (FrameLayout.java:194) 
com.android.internal.policy.PhoneWindow$DecorView.onMeasure (PhoneWindow.java:2643) 
android.view.View.measure (View.java:18788) 
android.view.ViewRootImpl.performMeasure (ViewRootImpl.java:2100) 
android.view.ViewRootImpl.measureHierarchy (ViewRootImpl.java:1216) 
android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1452) 
android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1107) 
android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:6013) 
android.view.Choreographer$CallbackRecord.run (Choreographer.java:858) 
android.view.Choreographer.doCallbacks (Choreographer.java:670) 
android.view.Choreographer.doFrame (Choreographer.java:606) 
android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:844) 
android.os.Handler.handleCallback (Handler.java:739) 
android.os.Handler.dispatchMessage (Handler.java:95) 

我在單獨CommentHolder.java文件ViewHolder公共:

public class CommentHolder extends RecyclerView.ViewHolder 
    implements View.OnClickListener { 
TextView tvComment = null; 
TextView tvUser = null; 
TextView tvDate = null; 
String emailComment = ""; 
String commentKey, catalogKey; 

FirebaseAuth auth; 
FirebaseDatabase database; 
CommentHolder(View row) { 
    super(row); 
    auth=FirebaseAuth.getInstance(); 
    database=FirebaseDatabase.getInstance(); 

    tvComment = (TextView) row.findViewById(R.id.comment); 
    tvUser = (TextView) row.findViewById(R.id.user); 
    tvDate = (TextView) row.findViewById(R.id.datecomment); 

    row.setOnClickListener(this); 
} 

@Override 
public void onClick(final View v) { 

    if (auth.getCurrentUser() == null || auth.getCurrentUser().getEmail() == null) { 

     Toast.makeText(v.getContext(), "Harap login terlebih dahulu", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    if (!auth.getCurrentUser().getEmail().equalsIgnoreCase(emailComment)) { 
     Toast.makeText(v.getContext(), "Anda hanya dapat mengedit komentar yang Anda buat", Toast.LENGTH_LONG).show(); 
     return; 
    } 


    final View form = ((Activity) v.getContext()).getLayoutInflater().inflate(R.layout.dialog_comment, null); 
    EditText etComment = (EditText) form.findViewById(R.id.editTextComment); 
    etComment.setText(tvComment.getText().toString()); 

    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); 

    builder 
      .setTitle("Komentar Anda") 
      .setView(form) 
      .setPositiveButton("Kirim", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        final EditText etComment = (EditText) form.findViewById(R.id.editTextComment); 
        DatabaseReference commentRef = database.getReference(MyConstants.FB_COMMENT) 
          .child(catalogKey).child(commentKey); 
        Comment comment = new Comment(auth.getCurrentUser().getUid() 
          , auth.getCurrentUser().getEmail(), etComment.getText().toString()); 
        commentRef.setValue(comment); 

       } 
      }) 
      .setNegativeButton("Batal", null) 
      .setNeutralButton("Hapus", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        DatabaseReference commentRef = database.getReference(MyConstants.FB_COMMENT) 
          .child(catalogKey).child(commentKey); 
        commentRef.removeValue(); 
       } 
      }) 
      .show(); 


} 

void bindModel(String catalogKey, String commentKey, String comment, String emailComment, String dateComment) { 
    tvComment.setText(comment); 
    String temp = emailComment.replaceAll("(?<=.).(?=[^@]*[email protected])", "*"); 
    tvUser.setText(temp); 
    tvDate.setText(dateComment); 
    this.emailComment = emailComment; 
    this.commentKey = commentKey; 
    this.catalogKey = catalogKey; 
} 

}

FirebaseRecyclerAdapter代碼:

mAdapter = new FirebaseRecyclerAdapter<Comment, CommentHolder>(Comment.class, R.layout.play_row 
      , CommentHolder.class, commentRef) { 
     @Override 
     public void populateViewHolder(CommentHolder commentHolder, Comment comment, int position) { 

      Date date = new Date((long) comment.getLastUpdate().get("date")); 
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); 
      String strDate = simpleDateFormat.format(date); 
      commentHolder.bindModel(catalogKey, getRef(position).getKey(), comment.getComment(), comment.getEmail(), strDate); 

     } 

    }; 

我Comment.java:

public class Comment { 
String uid; 
String email; 
String comment; 
HashMap<String, Object> lastUpdate; 

public Comment() { 
} 

public Comment(String uid, String email, String comment) { 
    this.uid=uid; 
    this.email = email; 
    this.comment = comment; 
    HashMap<String, Object> lastUpdateObj = new HashMap<>(); 
    lastUpdateObj.put("date", ServerValue.TIMESTAMP); 
    this.lastUpdate = lastUpdateObj; 


} 

public String getUid() { 
    return uid; 
} 

public String getEmail() { 
    return email; 
} 

public String getComment() { 
    return comment; 
} 

public HashMap<String, Object> getLastUpdate() { 
    return lastUpdate; 
} 
} 
+0

請修改您的問題以提供[mcve],包括與您的崩潰相關的Java堆棧跟蹤以及您實施的FirebaseRecyclerAdapter。 – CommonsWare

+0

@CommonsWare是的,謝謝。剛剛添加了java堆棧跟蹤和我的代碼 – Plugie

+2

只是一個猜測:嘗試使'CommentHolder'構造函數'public',看看會發生什麼。 – CommonsWare

回答

1

由於@CommonsWare猜測。將該公共添加到CommentHolder構造函數後,問題就解決了。

public CommentHolder(View row) { 
super(row); 
auth=FirebaseAuth.getInstance(); 
database=FirebaseDatabase.getInstance(); 

tvComment = (TextView) row.findViewById(R.id.comment); 
tvUser = (TextView) row.findViewById(R.id.user); 
tvDate = (TextView) row.findViewById(R.id.datecomment); 

row.setOnClickListener(this); 
} 
+0

@souttab請編輯您的評論,以提供一個最小,完整和可驗證的例子:) – Plugie

+0

仍然面臨問題 –

+0

雖然我的構造函數是公開的。因爲我想使用ProGuard,所以我使用minifyEnabled爲true。 – Sandra