EditTextPreference throws an Inflate exception

After about 1 hour of finding solutions to my problem in related matters, I decided to expose my case. Here it is: I have an InflateException every time I try to open my PreferenceActivity.

Magazine

E/AndroidRuntime: FATAL EXCEPTION: main Process: lineo.smarteam, PID: 5087 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{lineo.smarteam/lineo.smarteam.activity.SettingsActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class lineo.smarteam.preference.MyEditTextPreference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) 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 #5: Error inflating class lineo.smarteam.preference.MyEditTextPreference at android.preference.GenericInflater.createItem(GenericInflater.java:388) at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:432) at android.preference.GenericInflater.rInflate(GenericInflater.java:483) at android.preference.GenericInflater.rInflate(GenericInflater.java:495) at android.preference.GenericInflater.inflate(GenericInflater.java:327) at android.preference.GenericInflater.inflate(GenericInflater.java:264) at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273) at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:304) at lineo.smarteam.activity.SettingsActivity$SettingsFragment.onCreate(SettingsActivity.java:57) at android.app.Fragment.performCreate(Fragment.java:2198) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148) at android.app.BackStackRecord.run(BackStackRecord.java:793) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535) at android.app.FragmentController.execPendingActions(FragmentController.java:325) at android.app.Activity.performStart(Activity.java:6267) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) 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.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 

I have a settings screen with several EditTextPreferences, which allows me to configure some integer parameters.

Res \ XML \ preferences.xml

 <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/scores_category_title" android:key="pref_key_scores_settings"> <lineo.smarteam.preference.MyEditTextPreference android:title="@string/pref_title_win_score" android:inputType="numberSigned" android:maxLength="9" android:defaultValue="@integer/def_win_score" android:key="pref_key_win_score" > </lineo.smarteam.preference.MyEditTextPreference> (more of the same) </PreferenceCategory> </PreferenceScreen> 

Because I'm stubborn, and I want the cursor to align at the end of the text whenever I click any preferences, I created a custom EditTextPreference.

preferences \ MyEditTextPreference.java

 package lineo.smarteam.preference; import android.content.Context; import android.preference.EditTextPreference; import android.util.AttributeSet; import android.widget.EditText; public class MyEditTextPreference extends EditTextPreference { public MyEditTextPreference(Context context) { super(context); } public MyEditTextPreference(Context context, AttributeSet attrs) { super(context, attrs); } public MyEditTextPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onClick() { super.onClick(); EditText et = getEditText(); et.setSelection(et.getText().length()); } } 

As you can see, I have all the constructors mentioned mainly on all topics related to this problem. Then I have the actual PreferenceActivity:

activity \ SettingsActivity

 package lineo.smarteam.activity; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.util.Log; import android.view.MenuItem; import lineo.smarteam.MyApplication; import lineo.smarteam.R; public class SettingsActivity extends PreferenceActivity { static SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Context context = this; sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit(); } public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); setListeners(); } //EDIT: public void setListeners(){ setListenerA(); //other listeners } pulic void setListenerA(){ findPreference("KEY_PREF_WIN_SCORE").setOnPreferenceChangeListener() { //(...) } } } } 

As stated above, somewhere in this last missing piece of code, I have getActivity () calls, which, as I read somewhere, can cause this problem. The fact is that I tried to comment on all calls to this method, and the problem persists. Therefore, I came to the conclusion that this is not the reason. Also, I read that this method can throw a NullPointerException, so I always check it every time I use it.

I have a feeling that the decision is right in front of me, I just don’t see it. This was due to most of the related topics that I read.

Can someone help me find it?

thanks

EDIT:

Following Vijai's suggestion, I reinstalled the application. It still crashes in the same action, but the error has changed.

New magazine

 E/AndroidRuntime: FATAL EXCEPTION: main Process: lineo.smarteam, PID: 19210 Theme: themes:{} java.lang.RuntimeException: Unable to start activity ComponentInfo{lineo.smarteam/lineo.smarteam.activity.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) 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) 
+7
android android-preferences preferenceactivity inflate-exception
source share
2 answers

I found him.

I tried to find a preference whose identifier did not match the preferences.xml file.

It was a really stupid mistake. And that was in a piece of code that I did not share at first (sorry).

Anyway, thanks to everyone who tried to help!

+6
source share

The error indicates that you have an error in the MyEditTextPreference class.

 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 

I have this error before, but it can be very dependent on your situation. There are a few things you could try:

  • take a look at the MyEditTextPreference class and see if your xml layout matches the correct preference. the package should be the same . I made a mistake here.

  • try changing the class MyEditTextPreference , for example, delete some lines that I suggest:

    protected void onClick() { super.onClick(); EditText et = getEditText(); et.setSelection(et.getText().length()); }

or you can also change the constructor . See if you find a new error log.

  1. Create a gradle and clean up the project before deploying it to the device. usually this will indicate your error in xml.

If everything does not work, then the MyEditTextPreference class should be fixed. This is my suggestion to hope that this helps you.

+3
source share

All Articles