2017-01-12 13 views
1

FirebaseUIおよびそのrecyclerAdapterを使用して、次のJSON構造をrecyclerViewに表示しようとしています。FirebaseUI RecyclerAdapterを使用中にNoSuchMethodExceptionが発生しました

私は推測Firebaseチームによって行われた(ここでは、そこに自分自身の変化を伴う)の例を、以下、ここで見つけていますFirebase Quickstart - Database

JSON Firebaseは、構造:

{ 
    "restaurants": { 
     "pushID": { 
      "name": "string", 
      "address": "string", 
      "times": "string", 
      "logo": "string", 
      "phone": "string", 
      "email": "string", 
      "order": "true", //Boolean 
      "booking": "true", 
      "delivery": "false" 
     }, 
     ... 
    } 
} 

私はすでに見てきましたここと他のサイト、すなわちGitHubに "NoSuchMethodException"を受け取ることに関する現在の投稿。

一般的な答えは、このための2つの考えられる原因があることのようだ:

1)あなたのviewHolderクラスが公開されていることを確認してくださいあなたのTaskViewHolderは、例えばの内部クラスであれば

2)あなたの活動は、静的であることを確認してください。

ここに関連コードがあります。

モデル:

@IgnoreExtraProperties 
public class Restaurant { 

    public String name, address, 
      time, logo, phone; 
    public String uid; 
    public int favoriteCount = 0; 
    public Map<String, Boolean> favorites = new HashMap<>(); 

    public Restaurant(){ 
     // Default constructor required for calls to DataSnapshot.getValue(Restaurant.class) 
    } 

    public Restaurant(String name, String address, String time, 
          String logo, String phone, String uid){ 
     this.name = name; 
     this.address = address; 
     this.time = time; 
     this.logo = logo; 
     this.phone = phone; 
     this.uid = uid; 
    } 

    @Exclude 
    public Map<String, Object> toMap() { 
     HashMap<String, Object> result = new HashMap<>(); 
     result.put("uid", uid); 
     result.put("name", name); 
     result.put("time", time); 
     result.put("logo", logo); 
     result.put("favoriteCount", favoriteCount); 
     result.put("favorites", favorites); 

     return result; 
    } 
} 

マイviewHolderクラス(別のクラスであるための必要がない "静的")。

EllipsisUtil.classは、テキストビューを1行に制限したまま使用する方法です。何もない。

public class RestaurantViewHolder extends RecyclerView.ViewHolder { 

public TextView restaurantName, restaurantAddress; //tvDate, tvTime, tvPeople; 
public ImageView restaurantImageLogo, restaurantFavorite; 
private final Activity activity; 
private Locale locale = Locale.US; 
//private SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, MMMM dd yyyy", locale); 

public RestaurantViewHolder(Activity activity, View itemView){ 
    super(itemView); 
    this.activity = activity; 
    restaurantName = (TextView) itemView.findViewById(R.id.resName); 
    restaurantAddress = (TextView) itemView.findViewById(R.id.resAddress); 
    restaurantImageLogo = (ImageView) itemView.findViewById(R.id.RestaurantLogo); 
    restaurantFavorite = (ImageButton) itemView.findViewById(R.id.rFavorite); 
    //additional views for bookings activity 
    /* 
    tvDate = (TextView) itemView.findViewById(R.id.bookingDate); 
    tvTime = (TextView) itemView.findViewById(R.id.bookingTime); 
    tvPeople = (TextView) itemView.findViewById(R.id.numberPeople); 
    */ 
} 


public void bindToRestaurant(Restaurant restaurant, View.OnClickListener favoriteClickListener) { 
    //final int peopleNum = Integer.parseInt(restaurantInfo.booking_people); 
    restaurantName.setText(restaurant.name); 
    singleLineText(restaurantName); 
    restaurantAddress.setText(restaurant.address); 
    singleLineText(restaurantAddress); 
    restaurantFavorite.setOnClickListener(favoriteClickListener); 
    // Create a storage reference from our app 
    String dirName = "restaurantLogos/"; 
    String bucketUrl = "myFirebaseURL/"; 
    // Initialize Storage 
    StorageReference mStorageRef = FirebaseStorage.getInstance().getReference(); 
    StorageReference storageRef = mStorageRef.getStorage(). 
      getReferenceFromUrl(bucketUrl+dirName+restaurant.logo+".png"); 
    String logoUrl = storageRef.getDownloadUrl().toString(); 
    Glide.with(activity).load(logoUrl).into(restaurantImageLogo); 
    //additional views for bookings activity 
    /* 
    tvDate.setText(dateFormat.format(restaurantInfo.booking_date)); 
    tvTime.setText(restaurantInfo.booking_time); 
    String people; 
    if (peopleNum > 1){ 
     people = restaurantInfo.booking_people+" people"; 
    }else{ 
     people = restaurantInfo.booking_people+" person"; 
    } 
    tvPeople.setText(people); 
    */ 
} 

public void singleLineText(TextView textView){ 
    EllipsisUtil ellipsisUtil = new EllipsisUtil(textView); 
    ellipsisUtil.singleLineText(); 
} 
} 

Firebaseアダプタ

// Set up FirebaseRecyclerAdapter with the Query 
    Query restaurantQuery = getQuery(mDatabase); 
    mAdapter = new FirebaseRecyclerAdapter<Restaurant, RestaurantViewHolder>(Restaurant.class, R.layout.list_row, 
      RestaurantViewHolder.class, restaurantQuery) { 

     @Override 
     protected void populateViewHolder(final RestaurantViewHolder viewHolder, final Restaurant model, int position) { 
      final DatabaseReference restaurantRef = getRef(position); 

      final String restaurantKey = restaurantRef.getKey(); 
      //setting Strings data for intent data passing 

      //final String time = model.restaurant_time; 
      //final String phone = model.restaurant_phone; 

      //Log.d(TAG, "2: "+time+"/"+phone); 
      // Set click listener for the whole post view 
      viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //starting a new intent from onClick action 
        Context context = v.getContext(); 
        //RV = Restaurant View 
        Intent intentRV = new Intent(context, Restaurant.class); 
        intentRV.putExtra("rName", viewHolder.restaurantName.getText().toString()); 
        intentRV.putExtra("rAddress", viewHolder.restaurantAddress.getText().toString()); 
        //intentRV.putExtra("rTime",time); 
        //intentRV.putExtra("rPhone",phone); 
        //pass RID to next activity 
        intentRV.putExtra("resID", restaurantKey); 
        context.startActivity(intentRV); 
       } 
      }); 

      // Determine if the current user has liked this post and set UI accordingly 
      if (model.favorites.containsKey(getUid())) { 
       viewHolder.restaurantFavorite.setImageResource(R.drawable.ic_favorite_white_24dp); 
      } else { 
       viewHolder.restaurantFavorite.setImageResource(R.drawable.ic_favorite_border_white_24dp); 
      } 

      viewHolder.bindToRestaurant(model, new View.OnClickListener() { 
       @Override 
       public void onClick(View favoriteView) { 
        // Need to write to both places the post is stored 
        DatabaseReference globalRestaurantRef = mDatabase.child("favorites").child(restaurantRef.getKey()); 
        DatabaseReference userRestaurantRef = mDatabase.child("user_favorites").child(model.uid).child(restaurantRef.getKey()); 
        // Run two transactions 
        onFavoriteClicked(globalRestaurantRef); 
        onFavoriteClicked(userRestaurantRef); 
       } 
      }); 
     } 
    }; 
    recyclerView.setAdapter(mAdapter); 
} 

を扱うマイアクティビティクラスマイクエリーメソッド

public Query getQuery(DatabaseReference databaseReference){ 
    // All the restaurants 
    return databaseReference.child("restaurants");//restaurants 
} 

私が手Logcatエラー:

java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View] 
                       at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:171) 
                       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.dispatchLayout(RecyclerView.java:3254) 
                       at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1623) 
                       at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:331) 
                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
                       at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
                       at android.view.Choreographer.doFrame(Choreographer.java:549) 
                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
                       at android.os.Handler.handleCallback(Handler.java:739) 
                       at android.os.Handler.dispatchMessage(Handler.java:95) 
                       at android.os.Looper.loop(Looper.java:135) 
                       at android.app.ActivityThread.main(ActivityThread.java:5221) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at java.lang.reflect.Method.invoke(Method.java:372) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
                      Caused by: java.lang.NoSuchMethodException: <init> [class android.view.View] 
                       at java.lang.Class.getConstructor(Class.java:531) 
                       at java.lang.Class.getConstructor(Class.java:495) 
                       at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:168) 
                       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.dispatchLayout(RecyclerView.java:3254)  
                       at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1623)  
                       at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:331)  
                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)  
                       at android.view.Choreographer.doCallbacks(Choreographer.java:580)  
                       at android.view.Choreographer.doFrame(Choreographer.java:549)  
                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)  
                       at android.os.Handler.handleCallback(Handler.java:739)  
                       at android.os.Handler.dispatchMessage(Handler.java:95)  
                       at android.os.Looper.loop(Looper.java:135)  
                       at android.app.ActivityThread.main(ActivityThread.java:5221)  
                       at java.lang.reflect.Method.invoke(Native Method)  
                       at java.lang.reflect.Method.invoke(Method.java:372)  
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  

java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View] 
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:171) 
                      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.dispatchLayout(RecyclerView.java:3254) 
                      at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1623) 
                      at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:331) 
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
                      at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
                      at android.view.Choreographer.doFrame(Choreographer.java:549) 
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5221) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
                     Caused by: java.lang.NoSuchMethodException: <init> [class android.view.View] 
                      at java.lang.Class.getConstructor(Class.java:531) 
                      at java.lang.Class.getConstructor(Class.java:495) 
                      at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:168) 
                      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.dispatchLayout(RecyclerView.java:3254)  
                      at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1623)  
                      at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:331)  
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)  
                      at android.view.Choreographer.doCallbacks(Choreographer.java:580)  
                      at android.view.Choreographer.doFrame(Choreographer.java:549)  
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)  
                      at android.os.Handler.handleCallback(Handler.java:739)  
                      at android.os.Handler.dispatchMessage(Handler.java:95)  
                      at android.os.Looper.loop(Looper.java:135)  
                      at android.app.ActivityThread.main(ActivityThread.java:5221)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at java.lang.reflect.Method.invoke(Method.java:372)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)  

これは私のgrandleの依存関係であります:

dependencies { 
compile fileTree(include: ['*.jar'], dir: 'libs') 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:25.1.0' 
compile 'com.android.support:design:25.1.0' 
compile 'com.android.support:recyclerview-v7:25.1.0' 
compile 'com.github.bumptech.glide:glide:3.7.0' 
compile 'com.android.volley:volley:1.0.0' 
compile 'com.firebaseui:firebase-ui-database:1.0.0' 
compile 'com.google.firebase:firebase-database:10.0.1' 
compile 'com.google.firebase:firebase-storage:10.0.1' 
compile 'com.google.firebase:firebase-crash:10.0.1' 
compile 'com.google.firebase:firebase-auth:10.0.1' 
compile 'com.google.firebase:firebase-ads:10.0.1' 
compile 'com.google.firebase:firebase-invites:10.0.1' 
compile 'com.google.firebase:firebase-core:10.0.1' 
compile 'com.android.support:support-v4:25.1.0' 
compile 'com.google.firebase:firebase-appindexing:10.0.1' 
} 

これは私のprogaurd-ルールです:

-keepattributes Signature 
-keepattributes *Annotation* 
-keepattributes EnclosingMethod 
-keepattributes InnerClasses 

-keep class package.ViewHolder.** { 
    *; 
} 

-keepclassmembers class package.Models.** { 
    *; 
} 

私はこれまで運でそれを書いて、すべて数回試してみました。

解決策を見つけるのを手伝ってください。

+0

の可能な複製を[拡張パブリック静的である必要があります。

は、グラフの下に参照してください。 :java.lang.NoSuchMethodException: \ [class android.view.View \]](http://stackoverflow.com/questions/36972335/what-c​​ausing-this-exception-java-lang-runtimeexception-java-lang- nosuchmethodex) –

答えて

-1

レストランのクラスが間違っているようです。

分野で
public class Restaurant { 
    public PushID pushId; 

    //Constructor 

    //getter and setters for PushId 
    public void setPushId(PushId pushId) { 
     this.pushId = pushId; 
    } 

    public PushId getPushId() { 
     return pushId; 
    } 
} 

PushIdクラスこの

public class PushId { 

    public String name, address, 
      time, logo, phone; 
    public String uid; 
    public int favoriteCount = 0; 
    public Map<String, Boolean> favorites = new HashMap<>(); 

    public Restaurant() { 
    } 

    public Restaurant(String name, String address, String time, 
         String logo, String phone, String uid) { 
     this.name = name; 
     this.address = address; 
     this.time = time; 
     this.logo = logo; 
     this.phone = phone; 
     this.uid = uid; 
    } 

    @Exclude 
    public Map<String, Object> toMap() { 
     HashMap<String, Object> result = new HashMap<>(); 
     result.put("uid", uid); 
     result.put("name", name); 
     result.put("time", time); 
     result.put("logo", logo); 
     result.put("favoriteCount", favoriteCount); 
     result.put("favorites", favorites); 

     return result; 
    } 

使用のようにする必要がありますフィールド

+0

こんにちは。 PushIDはfirebaseによって '.push();'によって生成されます。方法。私はそれにセッターとゲッターが必要ないと思う。私がリンクしているFirebaseデータベースプロジェクトの例でも、pushIDのモデルは使用されていません。 – SiyaNM

+0

あなたは、残りのAPIからPushIdを取得している場合、あなたはPushIdが必要でしょう – kanduken

+0

どうですか? githubプロジェクトでFirebaseの開発者が提供するFirebase APIの使用例を見てください。私は上記を参照しました。 :) – SiyaNM

1
: あなたは

レストランクラスは、このようにする必要があり、完全なJSONを提供してくださいする必要があります

交換するだけです

compile 'com.firebaseui:firebase-ui-database:1.0.0' 
0123アプリレベルのbuild.gradleファイル内

compile 'com.firebaseui:firebase-ui-database:1.0.1' 

から

1.0.1 FirebaseUIバージョンが必要なFirebase/Play Servicesバージョン10.0.1を使用しています。これはあなたの問題を解決します。

> FirebaseUI Version Firebase/Play Services Version 
> 1.0.1    10.0.0 or 10.0.1 
> 1.0.0    9.8.0 
> 0.6.2    9.8.0 
> 0.6.1    9.6.1 
> 0.6.0    9.6.0 
> 0.5.3    9.4.0 
> 0.4.4    9.4.0 
> 0.4.3    9.2.1 
> 0.4.2    9.2.0 
> 0.4.1    9.0.2 
> 0.4.0    9.0.0 
+0

こんにちは。私は今試してみて、あなたに知らせるでしょう。 – SiyaNM

+0

運がよかったです。同じエラーが返されます。 – SiyaNM

+0

@SiyaLab:なぜあなたのケースでこれがうまくいかないのか分かりません。しかし、数日前に同じ例外が発生し、依存関係をアップグレードするだけで問題は解決しました。 –

-1

ViewHolderクラス

のpublic staticクラスRestaurantViewHolderはこの例外java.lang.RuntimeException原因は何RecyclerView.ViewHolder

関連する問題