我正在编写一个应用程序,其中一个活动是:按下按钮后,它会向数据库中保存的联系人发送电子邮件和短信。我在我的短信部分遇到了一些麻烦,因为应用程序一直在用空指针崩溃,我不知道我哪里出错...任何帮助都值得赞赏: 按下按钮时调用的方法:
public void onFinish() {
final String[] personalInfo = db.getPersonalDetails();
final Cursor contacts = db.getContacts();
if (match == false) {
sendSms();
if (db.hasGmail()) {
Thread s = new Thread(new Runnable() {
public void run() {
String args[] = db.getGmail();
GmailSender sender = new GmailSender(args[0],
args[1]);
Cursor c = db.getEmailContacts();
while (c.moveToNext()) {
try {
Log.e(args[0], args[1]);
sender.sendMail(
args[0],
c.getString(c
.getColumnIndex("emailAddress")));
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
}
});
s.start();
private void sendSms() {
sms = new Intent(this, SMS.class);
//sms.putExtra("message", message);
this.startService(sms);
}
SMS class:
public class SMS extends Service {
Context context;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);;
String BankAccount, BankNameAddress, SortCode;
String message;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void initilizePrefs() {
BankAccount = prefs.getString("BankAccount", null);
BankNameAddress = prefs.getString("BankNameAddress", null);
SortCode = prefs.getString("SortCode", null);
}
@Override
public void onStart(Intent intent, int startid) {
super.onStart(intent, startid);
initilizePrefs();
Bundle getvars = intent.getExtras();
if (getvars != null) {
message = getvars.getString("message");
}
char[] array = message.toCharArray();
message = message.replaceAll(" ", "");
String mes = "my account info is: " + BankNameAddress + " "
+ " account number: " + BankAccount + " Sort Code is: "
+ SortCode + " " + "Thank you so much!!";
Log.e("message", message);
try {
// if (BankNameAddress != null && BankAccount != null && SortCode !=
// null) {
sendSMS("Help!! I've completely run out of money and need you to send some via bank transfer please. "
+ mes);
}
/*
* } else Toast.makeText( getBaseContext(),
* "Please ensure all sections of preferences are filled",
* Toast.LENGTH_SHORT).show();}
*/
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void sendSMS(String message) {
Database db = new Database(this);
Cursor cursor = db.getNumbers();
db.onStop();
Log.e("message", message);
if (cursor != null) {
while (cursor.moveToNext()) {
String phoneNumber = cursor.getString(cursor
.getColumnIndex("number"));
Log.e("number", phoneNumber);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
}
}
logcat的:
03-08 15:32:25.784: E/AndroidRuntime(25227): FATAL EXCEPTION: main
03-08 15:32:25.784: E/AndroidRuntime(25227): java.lang.RuntimeException: Unable to instantiate service com.attack.android.SMS: java.lang.NullPointerException
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2380)
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.app.ActivityThread.access$1700(ActivityThread.java:134)
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.os.Looper.loop(Looper.java:137)
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.app.ActivityThread.main(ActivityThread.java:4867)
03-08 15:32:25.784: E/AndroidRuntime(25227): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 15:32:25.784: E/AndroidRuntime(25227): at java.lang.reflect.Method.invoke(Method.java:511)
03-08 15:32:25.784: E/AndroidRuntime(25227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-08 15:32:25.784: E/AndroidRuntime(25227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-08 15:32:25.784: E/AndroidRuntime(25227): at dalvik.system.NativeStart.main(Native Method)
03-08 15:32:25.784: E/AndroidRuntime(25227): Caused by: java.lang.NullPointerException
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
03-08 15:32:25.784: E/AndroidRuntime(25227): at com.attack.android.SMS.<init>(SMS.java:25)
03-08 15:32:25.784: E/AndroidRuntime(25227): at java.lang.Class.newInstanceImpl(Native Method)
03-08 15:32:25.784: E/AndroidRuntime(25227): at java.lang.Class.newInstance(Class.java:1319)
03-08 15:32:25.784: E/AndroidRuntime(25227): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2377)
03-08 15:32:25.784: E/AndroidRuntime(25227): ... 10 more
答案 0 :(得分:0)
在将i传递给context
之前,您忘记初始化getDefaultSharedPreferences
。这样做:
context=SMS.this;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
还在方法内而不是在类级别初始化prefs
。将initilizePrefs
方法更改为:
Context context;
SharedPreferences prefs;
public void initilizePrefs() {
context=SMS.this;
prefs = PreferenceManager
.getDefaultSharedPreferences(context);
//your code here....
}
答案 1 :(得分:0)
在SharedPreferences中使用它之前初始化context
。
context = getApplicationContext();
将此行移至onStart()
prefs = context.getSharedPreferences("NameOfSharedPreferences", <Mode>);
注意:在存储数据时,SharedPreferences的名称应与SharedPreferences的名称匹配。
在旁注中,最好在不同的java文件中编写Service。