0

我想在我的共享首选项中保存一些数据,但在保存时有以下例外。在行SharedPreferences.Editor store = DefaultData.edit();

05-29 20:20:53.234:E/AndroidRuntime(27650):致命异常:主要 05-29 20:20:53.234:E/AndroidRuntime(27650):java.lang.NullPointerException 05-29 20:20:53.234: E/AndroidRuntime(27650): at com.example.nirbhaya.DefaultDetails.onClick(DefaultDetails.java:92) 05-29 20:20:53.234: E/AndroidRuntime(27650): at android.view.View.performClick(View .java:2485) 05-29 20:20:53.234: E/AndroidRuntime(27650): 在 android.view.View$PerformClick.run(View.java:9080) 05-29 20:20:53.234: E/AndroidRuntime (27650): 在 android.os.Handler.handleCallback(Handler.java:587) 05-29 20:20:53.234: E/AndroidRuntime(27650): 在 android.os.Handler.dispatchMessage(Handler.java:92) 05-29 20:20:53.234: E/AndroidRuntime(27650): 在 android.os.Looper.loop(Looper.java:130) 05-29 20:20:53.234: E/AndroidRuntime(27650): 在 android.应用程序.ActivityThread。main(ActivityThread.java:3687) 05-29 20:20:53.234: E/AndroidRuntime(27650): at java.lang.reflect.Method.invokeNative(Native Method) 05-29 20:20:53.234: E/AndroidRuntime (27650): 在 java.lang.reflect.Method.invoke(Method.java:507) 05-29 20:20:53.234: E/AndroidRuntime(27650): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:867)05-29 20:20:53.234:E/AndroidRuntime(27650):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)05-29 20:20: 53.234:E/AndroidRuntime(27650):在 dalvik.system.NativeStart.main(本机方法)E/AndroidRuntime(27650): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 05-29 20:20:53.234: E/AndroidRuntime(27650): 在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:625) 05-29 20:20:53.234: E/AndroidRuntime(27650): at dalvik.system.NativeStart.main(Native Method)E/AndroidRuntime(27650): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 05-29 20:20:53.234: E/AndroidRuntime(27650): 在 com.android.internal .os.ZygoteInit.main(ZygoteInit.java:625) 05-29 20:20:53.234: E/AndroidRuntime(27650): at dalvik.system.NativeStart.main(Native Method)

Java代码 包com.example.nirbhaya;

import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class DefaultDetails extends Activity implements OnClickListener{

    Button save,reset;
    EditText dMob,dMsg,dEmail;
    String defMobNo,defMsg,defEmail;
    SharedPreferences DefaultData;

    private static final String TAG = "DD-Activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.defaultdetails);

        initializing();

    }
    private void initializing() {
        // TODO Auto-generated method stub


        save = (Button)findViewById(R.id.bsave1);
        reset = (Button)findViewById(R.id.bReset);

        dMob = (EditText)findViewById(R.id.etDefMobNo);
        dMsg = (EditText)findViewById(R.id.etDefMsg);
        dEmail = (EditText)findViewById(R.id.etDefEmail);

        save.setOnClickListener(this);
        reset.setOnClickListener(this);


    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId())
        {
        case R.id.bsave1:
            defMobNo = dMob.getText().toString();
            defMsg = dMsg.getText().toString();
            defEmail = dEmail.getText().toString();

                Log.i(TAG,"DONE");
                SharedPreferences.Editor store = DefaultData.edit();
                store.putString("defMobileNo", defMobNo);
                store.putString("defMessgae", defMsg);
                store.putString("defEMail", defEmail);
                store.commit();
                Intent openStartingPoint = new Intent (getApplicationContext(), CurrentDetails.class);
                startActivity(openStartingPoint);


            break;
        case R.id.bReset:
            ((EditText) findViewById(R.id.etDefMobNo)).setText("");
            ((EditText) findViewById(R.id.etDefEmail)).setText("");
            ((EditText) findViewById(R.id.etDefMsg)).setText("");


            break;
        }

    }



}
4

1 回答 1

3

你错过了初始化DefaultData

DefaultData = getSharedPreferences("SHARED_PREFS_NAME", Activity.MODE_PRIVATE);
于 2013-05-29T15:00:16.873 回答