0

在我的示例中,我在一项活动中输入手机号码、消息内容。在离开该活动之前,我将该信息保存在“共享偏好”中。在其他活动中,我试图让那些暴徒没有,消息,我能够得到没有但无法得到那个消息(第二个值)。请帮我解决这个问题。

DefaultDetails.java

package 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");
                DefaultData = getSharedPreferences("defMobileNo",0);
                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;
        }
    }
}

DefSMS.java

package com.example.nirbhaya;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class DefSms extends Activity{

    Button buttonSend;
    String defNo,defMsg;
    SharedPreferences DefaultData;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.defsms);

        DefaultData = getSharedPreferences("defMobileNo",0);
        final String defNo = DefaultData.getString("defMobileNo","Couldn't load data");

        DefaultData = getSharedPreferences("defMessgae",0);
        final String defMsg = DefaultData.getString("defMessgae","Couldn't load data");

        buttonSend = (Button) findViewById(R.id.buttonSend);

        buttonSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(defNo, null, defMsg, null, null);
                    Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),
                            "SMS faild, please try again later!",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }
        });

    }
}

这里def没有收到消息,因为无法加载数据 请帮助我

4

3 回答 3

0

替换为:

  DefaultData = getSharedPreferences("defMobileNo",0);
  final String defNo = DefaultData.getString("defMobileNo","Couldn't load data");

  final String defMsg = DefaultData.getString("defMessgae","Couldn't load data");
于 2013-05-30T04:40:14.390 回答
0

这就是共享偏好的基本工作方式

存储值

SharedPreferences preferences = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_WORLD_WRITEABLE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name","myNameisnothing");
editor.commit();

获取值

SharedPreferences prfs = getSharedPreferences("AUTHENTICATION_FILE_NAME", Context.MODE_PRIVATE);
String name = prfs.getString("Name", "");

这样,它会将名称的值返回为 myNameisnothing。PS.如果我错了,请纠正我。

于 2013-05-30T04:43:35.943 回答
0

DefaultData = getSharedPreferences("defMessgae",0);在 DefSMS.java中使用错误

请删除它,它会正常工作。

您试图获得名为 defMessgae 的共享首选项,它甚至不存在。因此,当您尝试访问它时,android 将创建一个具有默认值的新首选项。这就是您收到“无法加载数据”的原因

于 2013-05-30T04:53:37.577 回答