java.lang.IllegalStateException:无法执行活动的方法(共享首选项)
我正在尝试将值存储为应用程序的一些设置的共享首选项。但我现在已经走到了一条死胡同,我不知道为什么会出现这个错误,因为我现在正在使用Java/Android编码 我知道在SaveSettings方法中会发生这种情况,因为当我注释掉更新首选项并使用toast消息时,没有收到任何错误。从主活动设置选项调用设置活动。我已经找过了,找不到答案java.lang.IllegalStateException:无法执行活动的方法(共享首选项),java,android,Java,Android,我正在尝试将值存储为应用程序的一些设置的共享首选项。但我现在已经走到了一条死胡同,我不知道为什么会出现这个错误,因为我现在正在使用Java/Android编码 我知道在SaveSettings方法中会发生这种情况,因为当我注释掉更新首选项并使用toast消息时,没有收到任何错误。从主活动设置选项调用设置活动。我已经找过了,找不到答案 import android.app.Activity; import android.content.Context; import android.conten
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Settings extends Activity {
final String PREFS_NAME = "SettingsFile";
public static final String WebAddress = "webAddressKey";
public static final String Frequency = "frequencyKey";
TextView webUri;
SharedPreferences settings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_screen);
webUri = (TextView) findViewById(R.id.websiteUrl);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
// load spinner
Spinner spinner = (Spinner) findViewById(R.id.minutes);
// Create an ArrayAdapter using the string array and a default spinner
// layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.minutes,
android.R.layout.simple_spinner_dropdown_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
//get stored values
if (settings.contains(WebAddress)) {
webUri.setText(settings.getString(WebAddress, ""));
Log.e("log_tag", "Web Address Loaded");
}
//TO DO Retrieve Frequency
// if (settings.contains(Frequency)) {
// spinner.setText(settings.getString(Frequency, ""));
// Log.e("log_tag", "Web Address Loaded");
// }
getActionBar().setTitle("Settings");
}
public void saveSettings(View view) {
String weburi = webUri.getText().toString();
// Update Preferences
//Toast.makeText(getApplicationContext(), "Web URI = " + weburi, Toast.LENGTH_LONG).show();
Editor editor = settings.edit();
editor.putString(WebAddress, weburi);
editor.apply(); // commit the sharedPreferences.
}
}
我做错了什么 在
onCreate
中创建一个局部变量设置
。您已经创建了一个类成员settings
,所以您可能错过了这个
更改:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
到
在
onCreate
中创建一个局部变量settings
。您已经创建了一个类成员settings
,所以您可能错过了这个
更改:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
到
在
onCreate
中创建一个局部变量settings
。您已经创建了一个类成员settings
,所以您可能错过了这个
更改:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
到
在
onCreate
中创建一个局部变量settings
。您已经创建了一个类成员settings
,所以您可能错过了这个
更改:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
到
在调用
saveSettings()
时,webUri
是null
或webUri。getText()
返回null
。我不理解,因为我的Toast消息显示字符串webUri有值。在调用saveSettings()
时,webUri
是null
或webUri.getText()
返回null
。我不理解,因为我的Toast消息显示字符串webUri有值。在调用saveSettings()
时,webUri
是null
或webUri.getText()
正在返回null
。我不理解我的Toast消息显示字符串weburi有值。在调用saveSettings()
时,weburi
是null
或weburi.getText()
正在返回null
。我不明白我的Toast消息显示字符串weburi有值。好的,你能解释一下原因吗?我只是想了解为什么现在我删除了SharedReferences,它就起作用了。@Dino这是因为您在其中创建和设置了变量SharedReferences设置
是一个局部变量,只能在该范围(方法/局部范围)中访问。全局settings
变量可以在类实例中的任何位置访问。局部变量settings()会隐藏您的类成员设置变量,该变量在saveSettings()中为null,因此为NullPointerException。啊,是的,谢谢,我现在明白了。全球Vs本地,谢谢@Melquiades,这对我解释得更多。好的,你能解释为什么吗?我只是想了解为什么现在我删除了SharedReferences,它就起作用了。@Dino这是因为您在其中创建和设置了变量SharedReferences设置
是一个局部变量,只能在该范围(方法/局部范围)中访问。全局settings
变量可以在类实例中的任何位置访问。局部变量settings()会隐藏您的类成员设置变量,该变量在saveSettings()中为null,因此为NullPointerException。啊,是的,谢谢,我现在明白了。全球Vs本地,谢谢@Melquiades,这对我解释得更多。好的,你能解释为什么吗?我只是想了解为什么现在我删除了SharedReferences,它就起作用了。@Dino这是因为您在其中创建和设置了变量SharedReferences设置
是一个局部变量,只能在该范围(方法/局部范围)中访问。全局settings
变量可以在类实例中的任何位置访问。局部变量settings()会隐藏您的类成员设置变量,该变量在saveSettings()中为null,因此为NullPointerException。啊,是的,谢谢,我现在明白了。全球Vs本地,谢谢@Melquiades,这对我解释得更多。好的,你能解释为什么吗?我只是想了解为什么现在我删除了SharedReferences,它就起作用了。@Dino这是因为您在其中创建和设置了变量SharedReferences设置
是一个局部变量,只能在该范围(方法/局部范围)中访问。全局settings
变量可以在类实例中的任何位置访问。局部变量settings()会隐藏您的类成员设置变量,该变量在saveSettings()中为null,因此为NullPointerException。啊,是的,谢谢,我现在明白了。全球Vs本地,感谢@Melquiades,这对我解释得更清楚。