1

So i am trying to dynamically bring changes to the android:title of some preferences for the settings part, but my app keeps crashing when ever using .setText .setTitle and so.....

i have tried this Dynamically add EditTextPreference and Is possible to change the summary of EditTextPreference dynamically in Android? but did not work

Here is the code

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

Thats the fragment for the preference

Here is 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>

Here is the 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) 

well i dont think the problem is with the settingsActivity cause i have done many other test.... but Since SettingsActivity is being mention... here is the code for it

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();
    }
}
Community
  • 1
  • 1
Firifire
  • 45
  • 9
  • post your crash logcat please to see the exception – pleft Feb 01 '16 at 14:20
  • @elefasGR I have added the logcat – Firifire Feb 01 '16 at 14:39
  • How do you populate your `mList` in `SettingsActivity`? Are you sure it is not empty or null? Also what's the code in line SettingsActivity.java:32 ? can you post it? – pleft Feb 01 '16 at 14:48
  • @elefasGR well in the case when it is null.... basically no fingerprint. it should skip the first for loop, and then start the next where all the key ="Index" + i should have title="Empty Index" + (i+1) – Firifire Feb 01 '16 at 14:58
  • I have tried with situation without majority of the code and simply trying myTextView = (EditTextPreference) findPreference("Index1"); myTextView.setTitle("Empty Index") But still to no avail... is the problem perhaps in the xml? – Firifire Feb 01 '16 at 15:00
  • Ok from the logcat it seems that it fails to initialize the Spass object. You catch the exception (line 28...31 in SettingsActivity) but you do not print it (line 31). So your Spass object you try to access in line 32 has not been initialized hence the error in logcat occurs. You should place `isFeatureEnabled = mSpass.isFeatureEnabled(Spass.DEVICE_FINGERPRINT);` and all other relavitve code inside the try catch block. – pleft Feb 01 '16 at 15:01
  • @elefasGR that did not fix the problem.... i also removed all codes related to spass.... leaving the settingsactivity to start settingsfragment.... and in settingsfragment to have on this line `EditTextPreference[] myTextView = new EditTextPreference[10]; myTextView[1] = (EditTextPreference) findPreference("Index1"); myTextView[1].setTitle("something"); addPreferencesFromResource(R.xml.preferences);` And it still fails – Firifire Feb 01 '16 at 15:28
  • I didnt mention that this will resolve your problem, but it will indicate what the problem is. If you removed all relative code related to Spass object and any other proprietary sdk reference and it still crashes it should crash with another exception (not java.lang.IllegalStateException: initialize() is not Called first). please post the different exception logcat – pleft Feb 01 '16 at 15:31
  • @elefasGR i have updated the logcat, fragment and activity `EditTextPreference[] myTextView = new EditTextPreference[10]; myTextView[1] = (EditTextPreference) findPreference("Index1"); myTextView[1].setTitle("yo");` if i remove this from the fragment, the program runs.... but i want the title to change – Firifire Feb 01 '16 at 16:04

2 Answers2

2
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");
    }
}
Firifire
  • 45
  • 9
1

myTextView[1] = (EditTextPreference) findPreference("Index1");

This is probably wrong. Set myTextView[0] = (EditTextPreference) findPreference("Index1"); (zero index instead of 1)

or better check for null if((EditTextPreference) findPreference("Index1") != null) {..do your stuff here}

pleft
  • 7,567
  • 2
  • 21
  • 45
  • Thanks for your help.... i was able to solve the problem by moving all these expressions after addPreferencesFromResource(R.xml.preferences); – Firifire Feb 01 '16 at 17:12
  • oh yes, thats the reason for being null the EditTextPreference! didn't see that. – pleft Feb 01 '16 at 17:20