При попытке наполнить мой xml-файл я получаю исключение classcastex.Это мой первый проект для Android, поэтому я все еще учусь.Но я не знаю, как исправить свой код, чтобы я мог надуть мой класс "SummaryShowingEditTextPreference".При запуске моего кода я получаю следующую ошибку:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.flow, PID: 13800
android.view.InflateException: Binary XML file line #12: Error inflating class com.example.flow.displayClasses.AccountScreen.SummaryShowingEditTextPreference
at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:266)
at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:295)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:362)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:364)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:170)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:120)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:138)
at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:429)
at com.example.flow.displayClasses.AccountScreen.PreferenceFragmentAccount.onCreatePreferences(PreferenceFragmentAccount.java:25)
at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:228)
at android.support.v4.app.Fragment.performCreate(Fragment.java:2414)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1195)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1078)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:117)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2408)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ClassCastException: com.example.flow.displayClasses.AccountScreen.SummaryShowingEditTextPreference cannot be cast to android.support.v7.preference.Preference
at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:259)
Это мой класс, который вызывает файл PreferencesFragmentAccount:
public class AccountFragment extends Fragment {
private FragmentActivity myContext;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private View RootView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
private static final String TAG = AccountFragment.class.getSimpleName();
private static final String ARG_SECTION_NUMBER = "section_number";
public static final int TAB_PRIVATE = 1;
public static final int TAB_CORP = 2;
public AccountFragment() {
}
public static AccountFragment newInstance(int sectionNumber) {
AccountFragment fragment = new AccountFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RootView = inflater.inflate(R.layout.fragment_account, container, false);
Log.i(TAG, "onCreateView()");
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(myContext.getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) RootView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
//BUTTON
Button openSettingsButton = RootView.findViewById(R.id.open_settings);
openSettingsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent i = new Intent(getContext(), PreferenceFragmentAccount.class);
//startActivity(i);
Fragment preferenceFragmentAccount = new PreferenceFragmentAccount();
FragmentTransaction ft =myContext.getSupportFragmentManager().beginTransaction();
ft.add(R.id.relativelayout_for_fragment, preferenceFragmentAccount);
ft.commit();
}
});
return RootView;
}
И это тот класс, который ошибочно приведен, но яне знаю, как использовать это с android.support.v7:
public class SummaryShowingEditTextPreference extends EditTextPreference {
public SummaryShowingEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SummaryShowingEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SummaryShowingEditTextPreference(Context context) {
super(context);
}
// According to ListPreference implementation
@Override
public CharSequence getSummary() {
String text = getText();
if (TextUtils.isEmpty(text)) {
CharSequence hint = getEditText().getHint();
if (!TextUtils.isEmpty(hint) || super.getSummary().equals("%s")) {
return hint;
} else {
return super.getSummary();
}
} else {
CharSequence summary = super.getSummary();
if (!TextUtils.isEmpty(summary)) {
return String.format(summary.toString(), text);
} else {
return summary;
}
}
}
}
Любая помощь приветствуется!