gpt4 book ai didi

android - EditTextPreference 导致膨胀异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:24:39 25 4
gpt4 key购买 nike

在相关主题中寻找问题的解决方案大约 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。

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 的建议,我重新安装了应用程序。它仍然在同一操作中崩溃,但错误已更改。

新建日志

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)

最佳答案

我找到了。

我试图在 preferences.xml 文件中找到一个 ID 不匹配的首选项。

这真是一个愚蠢的错误。这是我没有先分享的一段代码(抱歉)。

无论如何,感谢所有试图提供帮助的人!

关于android - EditTextPreference 导致膨胀异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40517951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com