gpt4 book ai didi

android - 动态改变android :title in preference

转载 作者:行者123 更新时间:2023-11-29 17:24:35 25 4
gpt4 key购买 nike

所以我正在尝试动态更改设置部分的某些首选项的 android:title,但我的应用程序在使用 .setText .setTitle 等时一直崩溃......

我试过这个Dynamically add EditTextPreferenceIs possible to change the summary of EditTextPreference dynamically in Android?但没用

这是代码

public class SettingsFragment extends PreferenceFragment {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditTextPreference[] myTextView = new EditTextPreference[10];
myTextView[1] = (EditTextPreference) findPreference("Index1");
myTextView[1].setTitle("yo");
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}

这是偏好的 fragment

这是Preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Apps Selector"/>
<EditTextPreference
android:key="Index1"
android:title="Blank"
android:dialogTitle="Enter the App Package Name"
android:defaultValue=""/>
</PreferenceScreen>

这是日志

02-01 15:55:28.918 3103-3103/? I/art: Not late-enabling -Xcheck:jni (already on)
02-01 15:55:28.959 3103-3103/com.firifire W/System: ClassLoader referenced unknown path: /data/app/com.firifire-1/lib/x86
02-01 15:55:29.039 3103-3103/com.firifire D/AndroidRuntime: Shutting down VM
02-01 15:55:29.040 3103-3103/com.firifire E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.firifire, PID: 3103
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firifire/com.firifire.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.EditTextPreference.setTitle(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
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.NullPointerException: Attempt to invoke virtual method 'void android.preference.EditTextPreference.setTitle(java.lang.CharSequence)' on a null object reference
at com.firifire.SettingsFragment.onCreate(SettingsFragment.java:19)
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:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
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) 

好吧,我不认为问题出在 settingsActivity 上,因为我已经做了很多其他测试....但是由于提到了 SettingsActivity...这是它的代码

public class SettingsActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}

@Override
protected void onPause(){
super.onPause();
finish();
}
@Override
protected void onStop(){
super.onStop();
finish();
}

@Override
protected void onDestroy() {
super.onDestroy();
}
}

最佳答案

public class SettingsFragment extends PreferenceFragment {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
//Place after here
EditTextPreference[] myTextView = new EditTextPreference[10];
myTextView[1] = (EditTextPreference) findPreference("Index1");
myTextView[1].setTitle("yo");
}
}

关于android - 动态改变android :title in preference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35132624/

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