本文介绍了为什么会出现错误?:参数t的上限不兼容:首选项和首选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决以下错误的问题:

I can not solve the problem with the error:

incompatible types: no unique maximal instance exists for type variable T with upper bounds com.velocat.cmap.Preference,androidx.preference.Preference
        Preference preference = findPreference("button");
                                              ^
  where T is a type-variable:
    T extends androidx.preference.Preference declared in method <T>findPreference(CharSequence)

SettingsFragment.java

import android.os.Bundle;
import android.preference.Preference;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsFragmentActivity extends PreferenceFragmentCompat {

    Preference preference;
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

        addPreferencesFromResource(R.xml.setting);

        Preference preference = findPreference("preference1");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);

        return view;
    }

}

buld.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.1.0'
    implementation 'androidx.navigation:navigation-ui:2.1.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.preference:preference:1.1.0'

}

xml/settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <SwitchPreference
        android:defaultValue="false"
        android:key="preference1"
        android:title="test" />
</PreferenceScreen>

我重新阅读了搜索中的所有内容,但没有找到解决问题的方法.有谁知道如何摆脱这个错误?

I re-read everything possible in the search, but did not find a solution to the problem.Does anyone know how to get rid of this error?

感谢您对此主题的任何澄清.

I would be grateful for any clarification on this topic.

推荐答案

我猜您导入的错误首选项应该是

I guess you imported wrong Preference it should be

import androidx.preference.Preference

在扩展androidx.preference.PreferenceFragmentCompat时并按照文档它返回

androidx.preference.Preference

这篇关于为什么会出现错误?:参数t的上限不兼容:首选项和首选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 14:08