简体   繁体   English

EditTextPreference导致Inflate Exception

[英]EditTextPreference causes Inflate Exception

After about 1h looking for solutions to my problem in related topics, I decided to expose my case. 大约1小时后,在相关主题中寻找我的问题的解决方案,我决定公开我的案例。 Here it is: I'm having an InflateException everytime I try to open my PreferenceActivity. 这是:我每次尝试打开PreferenceActivity时都会出现InflateException。

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. 我有一个Preferences屏幕,有几个EditTextPreferences允许配置一些整数参数。

res\\xml\\preferences.xml 水库\\ 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. 因为我很顽固,每当我点击任何偏好时我都希望光标在文本的末尾对齐,我创建了一个自定义的EditTextPreference。

preference\\MyEditTextPreference.java 偏好\\ 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: 然后我有实际的PreferenceActivity:

activity\\SettingsActivity 活动\\ 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. 如上所述,在省略了最后一段代码的某处,我调用了getActivity(),正如我在某处读到的那样,可能会导致这个问题。 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. 另外,我已经读过该方法可能会抛出NullPointerException,因此我每次使用它时都会检查它。

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. 按照Vijai的建议,我重新安装了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)

I found it. 我找到了。

I was trying to find a preference whose id didn't match any in the preferences.xml file. 我试图找到一个首选项,其id与preferences.xml文件中的任何一个都不匹配。

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! 无论如何,感谢所有试图帮助的人!

The error said that you have an error in your MyEditTextPreference class. 该错误表示您的MyEditTextPreference类中存在错误。

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. 查看MyEditTextPreference类,看看你的布局xml是否调用了正确的首选项。 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 : 尝试修改你的MyEditTextPreference类,例如删除一些我的建议是:

    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. 在部署到设备之前,构建gradle并清理项目。 usually this will point your mistake in xml. 通常这会将你的错误指向xml。

If all not working then MyEditTextPreference class is where you should fix. 如果一切都不起作用,那么MyEditTextPreference类就是你应该修复的地方。 This is my suggestion hope it helps you. 这是我的建议希望它能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM