该应用程序编译成功,但是在单击任何EditTextPreference
时崩溃。
SettingsActivity
扩展了AppCompatActivity()
。
这是SettingsFragment
。
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.preference.PreferenceFragmentCompat
import android.widget.TextView
class SettingsFragment : PreferenceFragmentCompat() {
var key: String? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
key = rootKey
setPreferencesFromResource(R.xml.preferences, rootKey)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val topAppBarSettingsTitle: TextView = (activity as AppCompatActivity).findViewById(R.id.top_app_bar_settings_title)
topAppBarSettingsTitle.text = if (key == null) "Settings" else findPreference(key).title
}
}
这是/res/xml/preferences.xml
的相关部分。
...
<PreferenceCategory android:title="Personal info">
<PreferenceScreen
android:key="nickname_screen"
android:title="Nickname"
android:summary="Nickname">
<EditTextPreference
android:key="nickname"
android:title="Nickname"
android:dialogTitle="Nickname"
android:hint="Nickname"
android:persistent="true" />
...
</PreferenceScreen>
</PreferenceCategory>
...
这是堆栈跟踪。
FATAL EXCEPTION: main
Process: com.aleph.null.messaging, PID: 5167
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.requestFocus()' on a null object reference
at android.support.v7.preference.EditTextPreferenceDialogFragmentCompat.onBindDialogView(EditTextPreferenceDialogFragmentCompat.java:66)
at android.support.v7.preference.PreferenceDialogFragmentCompat.onCreateDialog(PreferenceDialogFragmentCompat.java:149)
at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1292)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2623)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2410)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2365)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2272)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案 0 :(得分:0)
简短的回答:请改用Android-Support-Preference-V7-Fix。
我遇到了类似的问题。
首先,android.support.v7.preference
和android.preference
并存,并且两者都有重叠的类,其中包括EditTextPreference
。混合使用它们会遇到许多不同的错误,而Android Studio会很乐意这样做。无论如何,我无法让EditTextPreference
与他们中的任何一个一起工作。
幸好有一个很好的固定库,位于上面。
请务必记住要进行更改:
PreferenceFragmentCompat
导入SettingsActivity
的地方EditTextPreference
至com.takisoft.fix.support.v7.preference.EditTextPreference
SettingsActivity
的主题。您可以在Android-Support-Preference-V7-Fix的自述文件中找到更多详细信息。