Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditTextPreference causes Inflate Exception

After about 1h looking for solutions to my problem in related topics, I decided to expose my case. Here it is: I'm having an InflateException everytime I try to open my PreferenceActivity.

Log

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 Preferences screen several EditTextPreferences allowing 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 preference, I created a custom EditTextPreference.

preference\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 in basically all the topics related with this issue. 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 the omitted this last piece of code, I have calls to getActivity() which, as I read somewhere, might cause this problem. The thing is I've tried commenting all calls to that method and the problem persists. Therefore I concluded that's not the cause. Also, I've read that that method might throw a NullPointerException so I'm always checking it everytime I use it.

I have a feeling the solution is right in front of me, I'm just not seeing it. It was the case for most related topics I've read.

Can someone help me find it?

Thanks

EDIT:

Following Vijai's suggestion, I reinstalled the App. It is still crashing in the same action, but the error has changed.

New Log

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)
like image 226
Marco Castanho Avatar asked Nov 09 '16 23:11

Marco Castanho


2 Answers

I found it.

I was trying to find a preference whose id didn't match any in the preferences.xml file.

It was really a silly mistake. And it was in a piece of code I didn't share ate first (sorry).

Anyway, thanks to everybody who tried to help!

like image 199
Marco Castanho Avatar answered Nov 16 '22 21:11

Marco Castanho


The error said that you have an error in your MyEditTextPreference class.

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

I have this error before but it may very depends on your situation. there are couple things you could try :

  1. look in your MyEditTextPreference class and see if your layout xml call the correct preference. the package must be the same. This is where I made mistake.

  2. try to modify your MyEditTextPreference class for example remove some line my suggestion is :

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

or you could also modify your constructor. See if you find new error log.

  1. Build gradle and clean your project, before you're deploying to device. usually this will point your mistake in xml.

If all not working then MyEditTextPreference class is where you should fix. This is my suggestion hope it helps you.

like image 3
Gujarat Santana Avatar answered Nov 16 '22 19:11

Gujarat Santana