所以我试图动态地对设置部分的某些首选项进行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);
    }
}


多数民众赞成在片段

这是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>


这是logCat

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");
    }
}

08-17 20:51