notifydatasetchanged()方法无法在Marshmallow上使用RecyclerView

时间:2017-12-25 07:25:05

标签: android android-recyclerview retrofit2 android-adapter

我的应用程序在 Android 6 上崩溃了 我使用自定义适配器RecyclerView进行Retrofit 当我致电notifydatasetchanged()时:方法无效RecyclerView

这是我的适配器类

AnswersAdapter.java

public class AnswersAdapter extends RecyclerView.Adapter<AnswersAdapter.ViewHolder> {

    private List<Item> mItems;
    private Context mContext;
    private PostItemListener mItemListener;


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        public TextView titleTv;
        PostItemListener mItemListener;

        public ViewHolder(View itemView, PostItemListener postItemListener) {
            super(itemView);
            titleTv =  itemView.findViewById(android.R.id.text1);

            this.mItemListener = postItemListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            Item item = getItem(getAdapterPosition());
            this.mItemListener.onPostClick(item.getAnswerId());

            notifyDataSetChanged();
        }
    }

    public AnswersAdapter(Context context, List<Item> posts, PostItemListener itemListener) {
        mItems = posts;
        mContext = context;
        mItemListener = itemListener;
    }

    @Override
    public AnswersAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        View postView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);

        ViewHolder viewHolder = new ViewHolder(postView, this.mItemListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(AnswersAdapter.ViewHolder holder, int position) {

        Item item = mItems.get(position);
        TextView textView = holder.titleTv;
        textView.setText(item.getOwner().getDisplayName());
    }

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

    public void updateAnswers(List<Item> items) {
        mItems.clear();
        mItems.addAll(items);
        this.notifyDataSetChanged();
    }

    private Item getItem(int adapterPosition) {
        return mItems.get(adapterPosition);
    }

    public interface PostItemListener {
        void onPostClick(long id);
    }
}

这是我的主要活动 MainActivity.java

public class MainActivity extends AppCompatActivity {

    private AnswersAdapter mAdapter;
    private SOService mService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mService = ApiUtils.getSOService();
        RecyclerView mRecyclerView =  findViewById(R.id.rvawnser);
        mAdapter = new AnswersAdapter(this, new ArrayList<Item>(0),
                new AnswersAdapter.PostItemListener() {

            @Override
            public void onPostClick(long id) {
                Toast.makeText(MainActivity.this, "Post id is" + id, Toast.LENGTH_SHORT).show();
            }
        });

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true);
        RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);
        loadAnswers();

    }

    public void loadAnswers() {
        mService.getAnswers().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SOAnswersResponse>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(SOAnswersResponse soAnswersResponse) {
                        mAdapter.updateAnswers(soAnswersResponse.getItems());
                    }
                });
    }
    private void showErrorMessage() {
        Toast.makeText(getApplicationContext(), "MainActivity: error loading from API", Toast.LENGTH_LONG).show();
    }

删除时它的工作正常

mAdapter.updateAnswers(soAnswersResponse.getItems());

这是错误日志

  

12-25 07:34:49.535 17676-17676 / com.example.masoudmk.myapplication   E / AndroidRuntime:致命异常:主要                                                                                       处理:com.example.masoudmk.myapplication,PID:17676                                                                                       java.lang.NoSuchMethodError:没有静态方法   的getFont(Landroid /内容/上下文; ILandroid / util的/的TypedValue; ILandroid /插件/ TextView的)Landroid /图形/字体或符号;   在课堂上Landroid / support / v4 / content / res / ResourcesCompat;或者它的超级   课程(宣言)   &#39; android.support.v4.content.res.ResourcesCompat&#39;出现在   /data/app/com.example.masoudmk.myapplication-1/split_lib_dependencies_apk.apk)                                                                                           在   android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119)                                                                                           在   android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:208)                                                                                           在   android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:110)                                                                                           在   android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)                                                                                           在   android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:81)                                                                                           在   android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:71)                                                                                           在   android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)                                                                                           在   android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)                                                                                           在   android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)                                                                                           在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)                                                                                           在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)                                                                                           在android.view.LayoutInflater.inflate(LayoutInflater.java:492)                                                                                           在android.view.LayoutInflater.inflate(LayoutInflater.java:423)                                                                                           在   com.example.masoudmk.myapplication.AnswersAdapter.onCreateViewHolder(AnswersAdapter.java:54)                                                                                           在   com.example.masoudmk.myapplication.AnswersAdapter.onCreateViewHolder(AnswersAdapter.java:13)                                                                                           在   android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:6519)                                                                                           在   android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5706)                                                                                           在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5589)                                                                                           在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5585)                                                                                           在   android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2231)                                                                                           在   android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)                                                                                           在   android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)                                                                                           在   android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:610)                                                                                           在   android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)                                                                                           在   android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3135)                                                                                           在android.view.View.measure(View.java:18788)                                                                                           在   android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)                                                                                           在   android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)                                                                                           在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:139)                                                                                           在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)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.example.masoudmk.myapplication"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        //noinspection GradleCompatible
        compile 'com.android.support:appcompat-v7:26.0.2'
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        compile 'com.android.support:support-v4:26.0.2'
        //noinspection GradleCompatible
        compile 'com.android.support:support-v13:26.0.2'
               // Retrofit
        compile 'com.squareup.retrofit2:retrofit:2.3.0'
         compile 'com.squareup.okhttp:okhttp:2.4.0'

        // JSON Parsing
        compile 'com.google.code.gson:gson:2.8.0'
        compile 'com.squareup.retrofit2:converter-gson:2.3.0'

    enter code here
        //noinspection GradleCompatible
        compile 'com.android.support:recyclerview-v7:26.0.2'

        testCompile 'junit:junit:4.12'

    compile 'io.reactivex:rxjava:1.1.6'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
}

问题是什么?

2 个答案:

答案 0 :(得分:0)

我认为问题在于支持库版本。检查app.gradle文件中的构建工具版本和依赖项版本。它们必须相同。

datetime

android: {
    ....
    buildToolsVersion: 'VERSION'
    ....
}

答案 1 :(得分:0)

以这种方式改变你的格斗:

buildToolsVersion "27.0.3"

dependencies {

...

compile 'com.android.support:support-v4:27.0.0'
compile 'com.android.support:appcompat-v7:27.0.0'
compile 'com.android.support:recyclerview-v7:27.0.0'
compile 'com.android.support:support-v13:27.0.0'
...


}