创建共享preferences对象之外的方法崩溃的应用程序 [英] Creating SharedPreferences object outside method crashes app
问题描述
为什么我需要创建一个新的共享preferences对象的每一个方法是用在(如的onClick的按钮)? 为什么我不能只是曾经在上课开始创建它,然后将它从任何方法来添加和删除它就像第二个例子吗? 如果我移动的方法以外的2排在第二个例子中,当活动应启动(当我去把它从另一个活动)崩溃直接与消息应用程序已意外停止 - 强行关闭。
示例1 - 这工作
公共类FormEdit延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
...
按钮btnSave =(按钮)findViewById(R.id.btnSave);
btnSave.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
共享preferences我的preF = getShared preferences(prefData,MODE_PRIVATE);
共享preferences.Editor我的prefEditor =我的pref.edit();
...
我的prefEditor.putString(钥匙,值);
我的prefEditor.commit();
...
示例2 - 不能正常工作
公共类FormEdit延伸活动{
共享preferences我的preF = getShared preferences(prefData,MODE_PRIVATE);
共享preferences.Editor我的prefEditor =我的pref.edit();
@覆盖
公共无效的onCreate(包savedInstanceState){
...
按钮btnSave =(按钮)findViewById(R.id.btnSave);
btnSave.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
...
我的prefEditor.putString(钥匙,值);
我的prefEditor.commit();
=更新= 登录猫:
E / AndroidRuntime(620):
未捕获的处理程序:螺纹主力退出,由于未捕获的异常
java.lang.RuntimeException的:无法实例活动ComponentInfo {com.devtest / com.devtest.FormEdit}:显示java.lang.NullPointerException
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
在android.app.ActivityThread.access $ 2200(ActivityThread.java:119)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:123)
在android.app.ActivityThread.main(ActivityThread.java:4363)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:521)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
在dalvik.system.NativeStart.main(本机方法)
显示java.lang.NullPointerException:产生的原因
在android.content.ContextWrapper.getShared preferences(ContextWrapper.java:146)
在com.devcom.android.devtest.IceEdit< INIT>(IceEdit.java:20)
在java.lang.Class.newInstanceImpl(本机方法)
在java.lang.Class.newInstance(Class.java:1479)
在android.app.Instrumentation.newActivity(Instrumentation.java:1021)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
... 11更多
在'getShared preferences()方法需要你的活动,这是后创建的背景下你的 super.onCreate( )
。所以,如果你之前实例化共享preferences
对象的的onCreate
,它会崩溃。
做你的活动,它在的onCreate
并使用它之后。
Why do I need to create a new SharedPreferences object for every method it is used in (like onClick for buttons)? Why can't I just create it once in the beginning of the class and then use it from any method to add and remove from it like in the second example? If I move the 2 rows outside the method as in second example, when the activity should start (when I go to it from another activity) it crashes directly with message "The application has stopped unexpectedly - Force close".
Example 1 - this works
public class FormEdit extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
...
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SharedPreferences myPref = getSharedPreferences("PrefData", MODE_PRIVATE );
SharedPreferences.Editor myPrefEditor = myPref.edit();
...
myPrefEditor.putString("Key", value);
myPrefEditor.commit();
...
Example 2 - doesn't work
public class FormEdit extends Activity {
SharedPreferences myPref = getSharedPreferences("PrefData", MODE_PRIVATE );
SharedPreferences.Editor myPrefEditor = myPref.edit();
@Override
public void onCreate(Bundle savedInstanceState) {
...
Button btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
...
myPrefEditor.putString("Key", value);
myPrefEditor.commit();
=update= log cat:
E/AndroidRuntime(620):
Uncaught handler: thread main exiting due to uncaught exception
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.devtest/com.devtest.FormEdit}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
at android.app.ActivityThread.access$2200(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
at com.devcom.android.devtest.IceEdit.<init>(IceEdit.java:20)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1479)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
... 11 more
The 'getSharedPreferences()' method needs the context of your Activity, which is created after your super.onCreate()
. So if you instantiate the SharedPreferences
object before your onCreate
, it'll crash.
Do it in onCreate
of your Activity and use it subsequently.
这篇关于创建共享preferences对象之外的方法崩溃的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!