1

I want to call the this method (the method is placed in the mainActivity):

public void setTextView(String[] countryArray, String column){
    TextView myAwesomeTextView;
    String textViewID;

    for(int i=0; i<=25; i++) {
        if(column.equals("left")){
            textViewID = "textViewColumn1_" + i;
        } else {
            textViewID = "textViewColumn2_" + i;
        }
        int resID = getResources().getIdentifier(textViewID, "id", getPackageName());
        myAwesomeTextView = (TextView) findViewById(resID);
        myAwesomeTextView.setText(countryArray[i]);
    }
}

If I call the method in my MainActivity their is no problem. But I want to call this in my AppCompatPreferenceActivity when the user change the settings of the app. But then the app crashes:

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    // handle the preference change here
    mainActivity.setTextView(usa, "left");
}

Android Monitor:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.larsus.test.testproject, PID: 56871
                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                              at android.content.ContextWrapper.getResources(ContextWrapper.java:85)
                                                                              at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
                                                                              at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:548)
                                                                              at com.larsus.test.testproject.ScrollingActivity.setTextView(ScrollingActivity.java:126)
                                                                              at com.larsus.test.testproject.AppCompatPreferenceActivity.onSharedPreferenceChanged(AppCompatPreferenceActivity.java:50)
                                                                              at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:476)
                                                                              at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:384)
                                                                              at android.preference.Preference.tryCommit(Preference.java:1433)
                                                                              at android.preference.Preference.persistString(Preference.java:1466)
                                                                              at android.preference.ListPreference.setValue(ListPreference.java:147)
                                                                              at android.preference.ListPreference.onDialogClosed(ListPreference.java:282)
                                                                              at android.preference.DialogPreference.onDismiss(DialogPreference.java:391)
                                                                              at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1292)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:135)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5349)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at java.lang.reflect.Method.invoke(Method.java:372)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

The question is why?

Saif
  • 723
  • 6
  • 21
Larsus
  • 101
  • 1
  • 12
  • How are you getting the reference to the `MainActivity` inside of the `PreferencesActivity`? and what is `scrollingActivity`? – riyaz-ali Feb 19 '18 at 12:03
  • sorry my fault. mainActivity is the scrollingActivity. I do it with MainActivity mainActivity = new MainActivity(); – Larsus Feb 19 '18 at 12:05
  • sorry my friend but you cannot instantiate an activity by yourself like this! only the Android system *knows* how to create an Activity. – riyaz-ali Feb 19 '18 at 12:09
  • but if this does not work how can I call a method from an other class? – Larsus Feb 19 '18 at 12:12
  • Make one public class with this method. in this class constructor add Activity activity. and use this class object to use this method – Vidhi Dave Feb 19 '18 at 12:13
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Michael Dodd Feb 19 '18 at 12:14
  • You cannot call a method on one from another activity! And it makes sense... why do you even want to update the TextView in an Activity that's not even visible to the user (let alone the technical part of Windows and Decor) – riyaz-ali Feb 19 '18 at 12:14
  • Check my answer – Vidhi Dave Feb 19 '18 at 12:25

2 Answers2

0

Try to call like this:

((MainActivity)getActivity()).setTextView(usa, "left");

I hope it works.

Orhan
  • 169
  • 1
  • 7
0

Make one public class with this method. In this class constructor add Activity object and use this class object to use this method.

    public class TextViewText {

        Activity mActivity;

        public TextViewText (Activity mActivity) {

             this.mActivity = mActivity;
        }

        public void setTextView(String[] countryArray, String column){
        TextView myAwesomeTextView;
        String textViewID;

        for(int i=0; i<=25; i++) {
            if(column.equals("left")){
                textViewID = "textViewColumn1_" + i;
            } else {
                textViewID = "textViewColumn2_" + i;
            }
            int resID = getResources().getIdentifier(textViewID, "id", getPackageName());
            myAwesomeTextView = (TextView) findViewById(resID);
            myAwesomeTextView.setText(countryArray[i]);
        }
    }

}

When you want to set the text :

TextViewText tvt = new TextViewText(MainActivity.this); //your activity context
tvt.setTextView(usa, "left");
Vidhi Dave
  • 5,614
  • 2
  • 33
  • 55