2013-05-30 107 views
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(); 
       } 

      } 
     }); 

    } 
} 

這裏沒有高清獲取消息爲無法加載數據 請幫我

+1

刪除此行** DefaultData = getSharedPreferences(「defMessgae」,0); **在DefSMS.java – Mahesh

+0

是的,現在它的工作,但如何? – KCRaju

+0

您試圖獲取名爲defMessgae的共享首選項,它甚至不存在。所以當你嘗試訪問它時,android會用缺省值創建一個新的首選項。這就是爲什麼你得到「無法加載數據」 – Mahesh

回答

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"); 
0

這是如何工作的基本sharedpreference

來存儲值

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", ""); 

這樣,它'd返回名稱的值myNameisnothing。 PS.Correct我如果我錯了。

0

您錯誤地在DefSMS.java

使用 DefaultData = getSharedPreferences("defMessgae",0);

請刪除這一點,它會正常工作。

您試圖獲取名爲defMessgae的共享首選項,該共享首選項甚至不存在。所以當你嘗試訪問它時,android會用缺省值創建一個新的首選項。這就是爲什麼你得到「無法加載數據」