大约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]
我有一个Preferences屏幕,有几个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。
偏好\ 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:
活性\ 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的建议,我重新安装了应用程序。它仍然在同一个动作中崩溃,但错误已经改变。
新记录
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)
答案 0 :(得分:6)
我找到了。
我试图找到一个偏好,其id与preferences.xml文件中的任何内容都不匹配。
这真是一个愚蠢的错误。它是在一段代码中我没有先分享吃的(对不起)。
无论如何,感谢所有试图提供帮助的人!
答案 1 :(得分:3)
错误表示您的MyEditTextPreference
课程出错。
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
之前我有这个错误,但这可能非常取决于你的情况。你可以尝试几件事:
查看您的MyEditTextPreference
课程,看看您的布局xml是否调用了正确的首选项。 包必须相同。这是我犯错的地方。
尝试修改您的MyEditTextPreference
类,例如删除一些我的建议:
protected void onClick() {
super.onClick();
EditText et = getEditText();
et.setSelection(et.getText().length());
}
或者您也可以修改构造函数。看看你是否找到了新的错误日志。
如果一切都不起作用,那么MyEditTextPreference
类就是你应该修复的地方。这是我的建议,希望它可以帮助你。