2016-07-22 98 views
0

我正在寫一個Android應用程序,它利用我使用RecyclerView製作的自定義適配器。該適配器工作正常,但是當我從它是運用它的活性,應用程序崩潰導航離開,並給了我這個錯誤:Android中的RecyclerView錯誤

07-22 14:30:41.037 11083-11083/com.epicodus.parkr E/AndroidRuntime: FATAL EXCEPTION: main 
                   Process: com.epicodus.parkr, PID: 11083 
                   java.lang.NoClassDefFoundError: android.support.v7.widget.RecyclerView$SavedState 
                    at android.support.v7.widget.RecyclerView.onSaveInstanceState(RecyclerView.java:1078) 
                    at android.view.View.dispatchSaveInstanceState(View.java:14641) 
                    at android.view.ViewGroup.dispatchFreezeSelfOnly(ViewGroup.java:3175) 
                    at android.support.v7.widget.RecyclerView.dispatchSaveInstanceState(RecyclerView.java:1109) 
                    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3161) 
                    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3161) 
                    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3161) 
                    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3161) 
                    at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:3161) 
                    at android.view.View.saveHierarchyState(View.java:14624) 
                    at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:2126) 
                    at android.app.Activity.onSaveInstanceState(Activity.java:1456) 
                    at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:564) 
                    at android.support.v7.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:502) 
                    at android.app.Activity.performSaveInstanceState(Activity.java:1383) 
                    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1286) 
                    at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:4473) 
                    at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3845) 
                    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3908) 
                    at android.app.ActivityThread.access$1200(ActivityThread.java:177) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                    at android.os.Looper.loop(Looper.java:145) 
                    at android.app.ActivityThread.main(ActivityThread.java:5942) 
                    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:1399) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

這裏是利用適配器的活動代碼:

package com.epicodus.parkr.ui; 

import android.content.Intent; 
import android.graphics.Typeface; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import com.epicodus.parkr.Constants; 
import com.epicodus.parkr.R; 
import com.epicodus.parkr.adapters.RentedSpotsAdapter; 
import com.epicodus.parkr.models.Spot; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.firebase.auth.FirebaseAuth; 
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 java.util.ArrayList; 

import butterknife.Bind; 
import butterknife.ButterKnife; 

public class AccountActivity extends AppCompatActivity implements View.OnClickListener { 

private FirebaseAuth mAuth; 
private String uid; 
private DatabaseReference mSpecificUserReference; 
private DatabaseReference mAllSpotsReference; 
private RentedSpotsAdapter mAdapter; 


@Bind(R.id.rentedSpotRecyclerView) RecyclerView mRecyclerView; 
@Bind(R.id.userNameDisplay) TextView mUserNameDisplay; 
@Bind(R.id.headline) TextView mHeadline; 
@Bind(R.id.logOutButton) Button mLogOutButton; 
@Bind(R.id.postSpotButton) Button mPostSpotButton; 
@Bind(R.id.findSpotsButton) Button mFindSpotsButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    mAuth = FirebaseAuth.getInstance(); 
    uid = mAuth.getCurrentUser().getUid(); 
    mSpecificUserReference = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_CHILD_USER).child(uid); 
    mAllSpotsReference = FirebaseDatabase.getInstance().getReference().child("spots"); 


    mSpecificUserReference.addValueEventListener(new ValueEventListener() { 

     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      String userName = dataSnapshot.child("fullName").getValue().toString(); 
      mUserNameDisplay.setText(userName); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

    mSpecificUserReference.child("rentedSpots").addValueEventListener(new ValueEventListener() { 
     ArrayList<Spot> mSpots = new ArrayList<>(); 
     ArrayList<String> spotKeys = new ArrayList<>(); 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for(DataSnapshot spotKeySnapshot : dataSnapshot.getChildren()){ 
       String spotKey = spotKeySnapshot.getValue().toString(); 
       spotKeys.add(spotKey); 
      } 
      mAllSpotsReference.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot AllSpotsDataSnapshot) { 
        for(DataSnapshot eachSpotSnapshot : AllSpotsDataSnapshot.getChildren()){ 
         String compareSpotId = eachSpotSnapshot.getKey(); 
         for(String eachSpotKey : spotKeys) 
          if(compareSpotId.equals(eachSpotKey)){ 
           String ownerId = eachSpotSnapshot.child("ownerID").getValue().toString(); 
           String address = eachSpotSnapshot.child("address").getValue().toString(); 
           String description = eachSpotSnapshot.child("description").getValue().toString(); 
           Double lat = Double.parseDouble(eachSpotSnapshot.child("latLng").child("latitude").getValue().toString()); 
           Double lng = Double.parseDouble(eachSpotSnapshot.child("latLng").child("longitude").getValue().toString()); 
           LatLng newLatLng = new LatLng(lat, lng); 
           String startDate = eachSpotSnapshot.child("startDate").getValue().toString(); 
           String startTime = eachSpotSnapshot.child("startTime").getValue().toString(); 
           String endDate = eachSpotSnapshot.child("endDate").getValue().toString(); 
           String endTime = eachSpotSnapshot.child("endTime").getValue().toString(); 
           Spot newSpot = new Spot(eachSpotKey, ownerId, address, description, newLatLng, startDate, startTime, endDate, endTime); 
           mSpots.add(newSpot); 
          } 
        } 
        mAdapter = new RentedSpotsAdapter(getApplicationContext(), mSpots); 
        mRecyclerView.setAdapter(mAdapter); 
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(AccountActivity.this); 
        mRecyclerView.setLayoutManager(layoutManager); 
        mRecyclerView.setHasFixedSize(true); 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 
        finish(); 
       } 
      }); 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      finish(); 
     } 
    }); 


    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_account); 
    ButterKnife.bind(this); 

    Typeface newFont = Typeface.createFromAsset(getAssets(), "fonts/Lobster-Regular.ttf"); 
    mHeadline.setTypeface(newFont); 

    Intent infoIntent = getIntent(); 
    String userName = infoIntent.getStringExtra("user"); 
    mUserNameDisplay.setText(userName); 

    mPostSpotButton.setOnClickListener(this); 
    mLogOutButton.setOnClickListener(this); 
    mFindSpotsButton.setOnClickListener(this); 
} 

private void logout() { 
    FirebaseAuth.getInstance().signOut(); 
    Intent intent = new Intent(AccountActivity.this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    startActivity(intent); 
    finish(); 
} 

@Override 
public void onClick(View view) { 
    if(view == mLogOutButton){ 
     logout(); 
    } else if (view == mPostSpotButton){ 
     Intent postSpotIntent = new Intent (AccountActivity.this, NewSpotActivity.class); 
     startActivity(postSpotIntent); 
    } else if (view == mFindSpotsButton){ 
     Intent findSpotsIntent = new Intent(AccountActivity.this, FindSpotsActivity.class); 
     startActivity(findSpotsIntent); 
    } 
} 
} 

這裏是適配器本身的代碼:

package com.epicodus.parkr.adapters; 

import android.content.Context; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.epicodus.parkr.R; 
import com.epicodus.parkr.models.Spot; 

import java.util.ArrayList; 

import butterknife.Bind; 
import butterknife.ButterKnife; 


public class RentedSpotsAdapter extends RecyclerView.Adapter<RentedSpotsAdapter.RentedSpotViewHolder> { 
private ArrayList<Spot> mSpots; 
private Context mContext; 

public RentedSpotsAdapter(Context context, ArrayList<Spot> spots){ 
    mSpots = spots; 
    mContext = context; 
} 

@Override 
public RentedSpotsAdapter.RentedSpotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rented_spot_list_item, parent, false); 
    RentedSpotViewHolder viewHolder = new RentedSpotViewHolder(view); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(RentedSpotsAdapter.RentedSpotViewHolder holder, int position) { 
    holder.bindSpot(mSpots.get(position)); 
} 

@Override 
public int getItemCount() { 
    return mSpots.size(); 
} 

public class RentedSpotViewHolder extends RecyclerView.ViewHolder { 

    @Bind(R.id.rentedSpotAddressDisplay) TextView mRentedSpotAddressDisplay; 
    @Bind(R.id.rentedSpotEndDateDisplay) TextView mRentedSpotEndDateDisplay; 
    @Bind(R.id.rentedSpotEndTimeDisplay) TextView mRentedSpotEndTimeDisplay; 

    private Context mContext; 

    public RentedSpotViewHolder(View itemView) { 
     super(itemView); 
     ButterKnife.bind(this, itemView); 
     mContext = itemView.getContext(); 
    } 

    public void bindSpot(Spot spot){ 
     mRentedSpotAddressDisplay.setText(spot.getAddress()); 
     mRentedSpotEndDateDisplay.setText(spot.getEndDate()); 
     mRentedSpotEndTimeDisplay.setText(spot.getEndTime()); 
    } 
} 
} 

這裏是應用級的build.gradle文件:

apply plugin: 'com.android.application' 


android { 
compileSdkVersion 24 
buildToolsVersion "24.0.0" 

defaultConfig { 
    applicationId "com.epicodus.parkr" 
    minSdkVersion 15 
    targetSdkVersion 24 
    versionCode 1 
    versionName "1.0" 
} 
packagingOptions { 
    exclude 'META-INF/DEPENDENCIES.txt' 
    exclude 'META-INF/DEPENDENCIES' 
    exclude 'META-INF/dependencies.txt' 
    exclude 'META-INF/LICENSE.txt' 
    exclude 'META-INF/LICENSE' 
    exclude 'META-INF/license.txt' 
    exclude 'META-INF/LGPL2.1' 
    exclude 'META-INF/NOTICE.txt' 
    exclude 'META-INF/NOTICE' 
    exclude 'META-INF/notice.txt' 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:24.0.0' 
compile 'com.jakewharton:butterknife:7.0.1' 
compile 'com.google.firebase:firebase-database:9.0.2' 
compile 'com.firebaseui:firebase-ui-database:0.4.1' 
compile 'com.google.firebase:firebase-auth:9.0.2' 
compile 'com.google.android.gms:play-services:9.0.2' 

compile 'com.android.support:recyclerview-v7:+' 
compile 'com.google.android.gms:play-services-appindexing:9.0.2' 
} 

configurations.all { 
resolutionStrategy { 
    force 'com.android.support:design:23.4.0' 
    force 'com.android.support:support-v4:23.4.0' 
    force 'com.android.support:appcompat-v7:23.4.0' 
} 
} 


apply plugin: 'com.google.gms.google-services' 

更新

我得到它的工作。它看起來像我只需要重寫onSaveInstanceState方法,並讓它爲空。

+0

因此,這個java.lang.NoClassDefFoundError:android.support.v7.widget.RecyclerView $ SavedState似乎指向 – Eenvincible

+0

請添加您的build.gradle你擁有recyclerview依賴 –

+0

添加build.gradle文件在主post –

回答

0

我只是猜測在這裏..你可以移動代碼,並立即放在oncreate()方法後,然後再試一次。

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_account);

+0

你的意思是在onCreate方法之外?導致它不會工作,如果它不在裏面onCreate –

+0

在onCreate()方法 – Avinash4551