无法对子类的DialogPreference进行充气

时间:2017-01-14 14:48:52

标签: android android-xml android-preferences android-inflate

好的,所以我试图在嵌套在AppCompatActivity中的F4中充气DialogPreference。每当我尝试访问活动主持人PreferenceFragment时,就会发生此错误。

PreferenceFragment

这是我的DialogPreferenceClass

01-14 09:40:37.002 29689-29689/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.santinocampos.android.count, PID: 29689
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.santinocampos.android.count/com.myname.android.count.Settings.SettingsActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #4: Error inflating class (not found)com.santinocampos.android.count.Settings.OptionDialogPreference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #4: Error inflating class (not found)com.santinocampos.android.count.Settings.OptionDialogPreference
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                   at com.santinocampos.android.count.Settings.SettingsActivity.onCreate(SettingsActivity.java:20)
                                                   at android.app.Activity.performCreate(Activity.java:6237)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:148) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                Caused by: android.view.InflateException: Binary XML file line #4: Error inflating class (not found)com.santinocampos.android.count.Settings.OptionDialogPreference
                                                   at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:300)
                                                   at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:358)
                                                   at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167)
                                                   at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117)
                                                   at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:115)
                                                   at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:366)
                                                   at com.santinocampos.android.count.Settings.SettingsFragment.onCreate(SettingsFragment.java:19)
                                                   at android.support.v4.app.Fragment.performCreate(Fragment.java:1951)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
                                                   at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
                                                   at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
                                                   at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                   at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
                                                   at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                   at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                   at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                   at com.santinocampos.android.count.Settings.SettingsActivity.onCreate(SettingsActivity.java:20) 
                                                   at android.app.Activity.performCreate(Activity.java:6237) 
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:148) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                Caused by: java.lang.ClassNotFoundException: Didn't find class "com.myname

这是我的Preference xml,其中通胀失败

import android.content.Context;
import android.preference.DialogPreference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

import com.myname.android.count.R;

import java.util.Set;

/**
 * Created by thedr on 1/13/2017.
 */

public class MyDialogPreference extends DialogPreference {

    public static final int KEY = R.string.preference_phoneNumber;

    private EditText mPhoneNumberEditText;

    public MyDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(false);
        setKey(getContext().getString(KEY));
        String summary = getSharedPreferences().getString(SettingsActivity.PHONE_NUMBER_KEY, "");
        setSummary(summary);
    }

    public void onBindDialogView(View view) {
        super.onBindDialogView(view);
        mPhoneNumberEditText = ((EditText) view.findViewById(R.id.editText_changeNumber));
    }

    public void onDialogClosed(boolean positive) {
        getSharedPreferences()
                .edit()
                .putString(SettingsActivity.PHONE_NUMBER_KEY, mPhoneNumberEditText.getText().toString())
                .apply();
        super.onDialogClosed(positive);
        persistBoolean(positive);
    }

}

这是我的PreferenceFragment

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <com.myname.android.count.Settings.OptionDialogPreference
        android:key="@string/preference_phoneNo"
        android:dialogIcon="@android:drawable/ic_dialog_alert"
        android:title="Reset Quests"
        android:summary="Reset all quest-progress."
        android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
        android:positiveButtonText="Clear Quests"
        android:negativeButtonText="Cancel"
        />

</PreferenceScreen>

这是我的主持人活动

package com.myname.android.count.Settings;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v7.preference.*;
import com.santinocampos.android.count.R;

/**
 * Created by thedr on 1/13/2017.
 */

public class SettingsFragment extends PreferenceFragmentCompat {

    private static final String TAG = "SettingsFragment";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    }

    @Override
    public void onDisplayPreferenceDialog(Preference preference) {
        super.onDisplayPreferenceDialog(preference);

        if (preference.getKey().equals(getContext().getString(MyDialogPreference.KEY))) {

        }
    }

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {

    }

    public static String getTagString() {
        return TAG;
    }

}

0 个答案:

没有答案