我正在尝试使用一些由hidron编写的首选项类。我将SpinnerPrefence类添加到preference.xml,但是我运行该应用程序,它给了我InflateException错误。问题是为什么我会收到此错误!我在xml文件中检查了程序包名称,可以。我进行了搜索以找到解决方案,并在SpinnerPreference类中添加了完整的构造函数,但找不到问题。有什么解决办法吗?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.payachip.automatedgreenhouse, PID: 14777
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.payachip.automatedgreenhouse/com.payachip.automatedgreenhouse.Activities.ActivitySettings}: android.view.InflateException: Binary XML file line #12: Error inflating class com.payachip.automatedgreenhouse.Preferences.SpinnerPreference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.payachip.automatedgreenhouse.Preferences.SpinnerPreference
at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:258)
at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:287)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:354)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:356)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:162)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:112)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:137)
at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:432)
at com.payachip.automatedgreenhouse.Fragments.FragmentPreference.onCreatePreferences(FragmentPreference.java:35)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:224)
at com.payachip.automatedgreenhouse.Fragments.FragmentPreference.onCreate(FragmentPreference.java:29)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2331)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1386)
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at android.app.Activity.performStart(Activity.java:7029)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.InstantiationException: Can't instantiate abstract class com.payachip.automatedgreenhouse.Preferences.SpinnerPreference
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:251)
... 35 more
SpinnerPreference.java
package com.payachip.automatedgreenhouse.Preferences;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import com.payachip.automatedgreenhouse.R;
public abstract class SpinnerPreference extends Preference {
protected String[] mEntries = new String[0];
protected String[] mEntryValues = new String[0];
private int mSelection = 0;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public SpinnerPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
public SpinnerPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public SpinnerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public SpinnerPreference(Context context) {
super(context);
init(context, null);
}
private void init(Context context, AttributeSet attrs) {
setWidgetLayoutResource(R.layout.preference_spinner);
TypedArray ta;
if(attrs != null){
ta = context.obtainStyledAttributes(attrs, R.styleable.SpinnerPreference);
}else {
ta = context.obtainStyledAttributes(R.styleable.SpinnerPreference);
}
int entriesResId = ta.getResourceId(R.styleable.SpinnerPreference_entries, 0);
if (entriesResId != 0) {
mEntries = context.getResources().getStringArray(entriesResId);
}
int valuesResId = ta.getResourceId(R.styleable.SpinnerPreference_entryValues, 0);
if (valuesResId != 0) {
mEntryValues = context.getResources().getStringArray(valuesResId);
}
ta.recycle();
}
}
这是我的preference.xml
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.preference.PreferenceCategory
android:title="General">
<com.payachip.automatedgreenhouse.Preferences.SpinnerPreference
android:key="LANGUAGE_KEY"
android:title="@string/pref_language_title"
android:defaultValue="0"
app:entries="@array/pref_language_type_options"
app:entryValues="@array/pref_language_type_values" />
</android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
ActivitySetting.java
public class ActivitySettings extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
if (savedInstanceState == null) {
// Display the fragment as the main content.
FragmentPreference fragmentPreference = FragmentPreference.newInstance();
fragmentPreference.setArguments(this.getIntent().getExtras());
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragmentPreference).commit();
}
}
}
FragmentPreference.java
public class FragmentPreference extends PreferenceFragmentCompat {
public static FragmentPreference newInstance(){
FragmentPreference fr = new FragmentPreference();
return fr;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
addPreferencesFromResource(R.xml.preference);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
谢谢。