我正在尝试将值存储为应用的一些设置的共享首选项。但我现在已经陷入了死胡同,我不知道为什么我会收到此错误,因为我现在正在使用Java / Android编码。
我知道这会发生在SaveSettings方法中,因为当我注释掉更新首选项并使用toast消息时,我没有收到任何错误。从主活动设置选项调用设置活动。我搜索过,找不到答案。
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.
}
}
与此相关的xml如下所示,请注意,一旦我将整个应用程序按照我的意愿工作,我将修复硬编码字符串。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settingsScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Please enter your web site url as www.yoursitename.com, please don't enter the http:// prefix" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:text="http://" />
<EditText
android:id="@+id/websiteUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:ems="10" />
<Spinner
android:id="@+id/minutes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/websiteUrl"
android:layout_below="@+id/textView3" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:text="Enter Frequency of Updates in Minutes" />
<Button
android:id="@+id/submit_botton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/minutes"
android:layout_below="@+id/minutes"
android:onClick="saveSettings"
android:text="Save Values" />
</RelativeLayout>
</ScrollView>
我得到的日志是:
01-10 11:12:01.791: E/AndroidRuntime(5491): FATAL EXCEPTION: main
01-10 11:12:01.791: E/AndroidRuntime(5491): Process: uk.co.diong.getonline, PID: 5491
01-10 11:12:01.791: E/AndroidRuntime(5491): java.lang.IllegalStateException: Could not execute method of the activity
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.view.View$1.onClick(View.java:3823)
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.view.View.performClick(View.java:4438)
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.view.View$PerformClick.run(View.java:18422)
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.os.Handler.handleCallback(Handler.java:733)
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.os.Handler.dispatchMessage(Handler.java:95)
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.os.Looper.loop(Looper.java:136)
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-10 11:12:01.791: E/AndroidRuntime(5491): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 11:12:01.791: E/AndroidRuntime(5491): at java.lang.reflect.Method.invoke(Method.java:515)
01-10 11:12:01.791: E/AndroidRuntime(5491): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-10 11:12:01.791: E/AndroidRuntime(5491): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-10 11:12:01.791: E/AndroidRuntime(5491): at dalvik.system.NativeStart.main(Native Method)
01-10 11:12:01.791: E/AndroidRuntime(5491): Caused by: java.lang.reflect.InvocationTargetException
01-10 11:12:01.791: E/AndroidRuntime(5491): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 11:12:01.791: E/AndroidRuntime(5491): at java.lang.reflect.Method.invoke(Method.java:515)
01-10 11:12:01.791: E/AndroidRuntime(5491): at android.view.View$1.onClick(View.java:3818)
01-10 11:12:01.791: E/AndroidRuntime(5491): ... 11 more
01-10 11:12:01.791: E/AndroidRuntime(5491): Caused by: java.lang.NullPointerException
01-10 11:12:01.791: E/AndroidRuntime(5491): at uk.co.diong.getonline.Settings.saveSettings(Settings.java:55)
01-10 11:12:01.791: E/AndroidRuntime(5491): ... 14 more
我做错了什么?
答案 0 :(得分:3)
在onCreate
中,您创建了一个本地变量settings
。您已经创建了一个类成员settings
,所以您可能错过了这个。
变化:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
到
settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);