EditTextPreference 导致膨胀异常
EditTextPreference causes Inflate Exception
在相关主题中寻找我的问题的解决方案大约 1 小时后,我决定公开我的案例。就是这样:每次我尝试打开 PreferenceActivity 时都会遇到 InflateException。
日志
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]
我有一个首选项屏幕,几个 EditTextPreferences 允许配置一些整数参数。
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>
因为我很固执,而且我希望每当我单击任何首选项时光标都与文本的末尾对齐,所以我创建了一个自定义的 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());
}
}
如您所见,本期相关话题中提到的构造函数我都有。
然后我有实际的 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() {
//(...)
}
}
}
}
如上所述,在省略最后一段代码的某处,我调用了 getActivity(),正如我在某处读到的那样,这可能会导致此问题。问题是我已经尝试评论对该方法的所有调用,但问题仍然存在。因此我得出结论,这不是原因。另外,我读到该方法可能会抛出 NullPointerException,所以我每次使用它时都会检查它。
我感觉解决方案就在我面前,只是我没有看到。我读过的大多数相关主题都是这种情况。
有人可以帮我找到吗?
谢谢
编辑:
按照Vijai的建议,我重新安装了App。还是在同一个动作中崩溃,但是报错变了。
新建日志
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)
错误说您的 MyEditTextPreference
class 有错误。
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
我以前遇到过这个错误,但这可能取决于您的情况。您可以尝试几件事:
查看您的 MyEditTextPreference
class 并查看您的布局 xml 是否调用了正确的首选项。 包裹必须相同。这是我弄错的地方。
尝试修改您的 MyEditTextPreference
class 例如删除一些行我的建议是:
protected void onClick() {
super.onClick();
EditText et = getEditText();
et.setSelection(et.getText().length());
}
或者你也可以修改你的构造函数。查看是否发现新的错误日志。
- 构建 gradle 并清理您的项目,然后再部署到设备。通常这会指出你在 xml.
中的错误
如果都不起作用,那么 MyEditTextPreference
class 就是您应该修复的地方。这是我的建议希望对你有帮助。
我找到了。
我试图在 preferences.xml 文件中找到其 ID 不匹配的首选项。
这真是一个愚蠢的错误。这是我没有先分享的一段代码(抱歉)。
无论如何,感谢所有试图提供帮助的人!
在相关主题中寻找我的问题的解决方案大约 1 小时后,我决定公开我的案例。就是这样:每次我尝试打开 PreferenceActivity 时都会遇到 InflateException。
日志
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]
我有一个首选项屏幕,几个 EditTextPreferences 允许配置一些整数参数。
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>
因为我很固执,而且我希望每当我单击任何首选项时光标都与文本的末尾对齐,所以我创建了一个自定义的 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());
}
}
如您所见,本期相关话题中提到的构造函数我都有。 然后我有实际的 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() {
//(...)
}
}
}
}
如上所述,在省略最后一段代码的某处,我调用了 getActivity(),正如我在某处读到的那样,这可能会导致此问题。问题是我已经尝试评论对该方法的所有调用,但问题仍然存在。因此我得出结论,这不是原因。另外,我读到该方法可能会抛出 NullPointerException,所以我每次使用它时都会检查它。
我感觉解决方案就在我面前,只是我没有看到。我读过的大多数相关主题都是这种情况。
有人可以帮我找到吗?
谢谢
编辑:
按照Vijai的建议,我重新安装了App。还是在同一个动作中崩溃,但是报错变了。
新建日志
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)
错误说您的 MyEditTextPreference
class 有错误。
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
我以前遇到过这个错误,但这可能取决于您的情况。您可以尝试几件事:
查看您的
MyEditTextPreference
class 并查看您的布局 xml 是否调用了正确的首选项。 包裹必须相同。这是我弄错的地方。尝试修改您的
MyEditTextPreference
class 例如删除一些行我的建议是:protected void onClick() { super.onClick(); EditText et = getEditText(); et.setSelection(et.getText().length()); }
或者你也可以修改你的构造函数。查看是否发现新的错误日志。
- 构建 gradle 并清理您的项目,然后再部署到设备。通常这会指出你在 xml. 中的错误
如果都不起作用,那么 MyEditTextPreference
class 就是您应该修复的地方。这是我的建议希望对你有帮助。
我找到了。
我试图在 preferences.xml 文件中找到其 ID 不匹配的首选项。
这真是一个愚蠢的错误。这是我没有先分享的一段代码(抱歉)。
无论如何,感谢所有试图提供帮助的人!