我正在为大学编写一个应用程序,其中一个活动通过短信和电子邮件在后台发送(按钮点击)保存在首选项中的银行信息。目前电子邮件将发送,但保存在首选项中的信息将以null发送值:
发送电子邮件方法:
public synchronized void sendMail(String loc, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
//loc = loc.replace(" ", "");
String mes = "Help!! I've completely run out of money and need you to send some via bank transfer please. "
+ "my account info is: "
+ BankNameAddress
+ " "
+ " account number: "
+ BankAccount
+ " Sort Code is: "
+ SortCode
+ " "
+ "Thank you so much!!";
DataHandler handler = new DataHandler(new ByteArrayDataSource(mes.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
message.setSubject("Help Money Emergency!");
message.setDataHandler(handler);
addAttachment(mes);
message.setContent(_multipart);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}catch(Exception e){
}
}
首选项java:
package com.attack.android;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity{
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
首选项xml:
<EditTextPreference
android:key="BankAccount"
android:summary="Enter your bank account number"
android:title="Bank account Number" >
</EditTextPreference>
<EditTextPreference
android:key="BankNameAddress"
android:summary="Enter your banks name and address"
android:title="Bank name and address" >
</EditTextPreference>
<EditTextPreference
android:key="SortCode"
android:summary="Enter your bank sort code"
android:title="Sort Code" >
</EditTextPreference>
答案 0 :(得分:0)
我认为您没有正确填充您的偏好值。以下是你应该怎么做的。
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String bankAccount=prefs.getString("BankAccount","");
...
第一个值是您首选项中的键,第二个值是默认值,以防先前没有输入任何内容。
另外,作为一种风格问题,只有类应以大写字母开头,变量应以小写字母开头。它只是更容易弄清楚发生了什么。 BankAccount
就是这样一个例子。