gpt4 book ai didi

android - 无法在PreferenceActivity中调用Android Studio方法

转载 作者:行者123 更新时间:2023-12-03 16:05:43 25 4
gpt4 key购买 nike

我想调用此方法(该方法放置在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 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)

问题是为什么?

最佳答案

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

}

要设置文本时:
TextViewText tvt = new TextViewText(MainActivity.this); //your activity context
tvt.setTextView(usa, "left");

关于android - 无法在PreferenceActivity中调用Android Studio方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48865742/

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