【问题标题】:Android Studio Method can not be called in PreferenceActivity在 PreferenceActivity 中无法调用 Android Studio 方法
【发布时间】:2018-07-29 15:54:52
【问题描述】:

我要调用this方法(方法放在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]);
    }
}

如果我在 MainActivity 中调用该方法,它们是没有问题的。 但是当用户更改应用程序的设置时,我想在我的 AppCompatPreferenceActivity 中调用它。但随后应用程序崩溃:

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

Android 监视器:

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)

问题是为什么?

【问题讨论】:

  • 你是如何在PreferencesActivity 中获得对MainActivity 的引用? scrollingActivity 是什么?
  • 对不起我的错。 mainActivity 是滚动活动。我用 MainActivity mainActivity = new MainActivity();
  • 对不起我的朋友,但你不能像这样自己实例化一个活动!只有 Android 系统知道如何创建 Activity。
  • 但是如果这不起作用,我该如何调用其他类的方法?
  • 用这个方法创建一个公共类。在此类构造函数中添加 Activity 活动。并使用这个类对象来使用这个方法

标签: android crash settings preferenceactivity


【解决方案1】:

使用此方法创建一个公共类。在此类构造函数中添加 Activity对象和使用这个类对象来使用这个方法。

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

}

当你想设置文字时:

TextViewText tvt = new TextViewText(MainActivity.this); //your activity context
tvt.setTextView(usa, "left");

【讨论】:

  • 乐于助人!快乐编码:)
  • 一旦你获得足够的声誉,别忘了点赞:P
【解决方案2】:

尝试这样调用:

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

我希望它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2016-04-16
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多