编辑:解决办法是在我的吐司中将Main.this替换为null。
我正在使用AsyncTask发送短信并且它们都已成功发送(我已经测试过发送50给自己)但是当我按发送时应用程序立即强制关闭。我使用了断点,我决定在“sms.sendTextMessage ...”循环后关闭应用程序。这是我的main.java。
package com.dillonregi.spamsms;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}
public void sms(View view) {
Toast.makeText(this, "sms()", Toast.LENGTH_SHORT).show();
final Handler mHandler = new Handler();
new AsyncTask<String, Integer, Long>() {
protected Long doInBackground(String... params){
EditText number = (EditText) Main.this.findViewById(R.id.pnumber);
EditText message = (EditText) Main.this.findViewById(R.id.pmessage);
EditText hmany = (EditText) Main.this.findViewById(R.id.hmany);
String _hmany = hmany.getText().toString();
final String _number = number.getText().toString();
final String _message = message.getText().toString();
final int xtime = Integer.parseInt(_hmany);
mHandler.post(new Runnable() {
public void run() {
Toast.makeText(null, "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
}
});
long sentTexts = 0;
for (int i = 0; i < xtime; i++) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(_number, null, _message, null, null);
//send SMS's
sentTexts += 1;
}
return sentTexts;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
Toast.makeText(null, "number of texts sent will end up going here. maybe add + sentTexts after these quotes?", Toast.LENGTH_LONG).show();
}
}.execute(); }
}
和LogCat
07-15 16:09:39.152:W / dalvikvm(29508):threadid = 1:线程退出,未捕获异常(group = 0x40019560) 07-15 16:09:39.152:E / AndroidRuntime(29508):致命异常:主要 07-15 16:09:39.152:E / AndroidRuntime(29508):java.lang.NullPointerException 07-15 16:09:39.152:E / AndroidRuntime(29508):在android.widget.Toast。(Toast.java:89) 07-15 16:09:39.152:E / AndroidRuntime(29508):在android.widget.Toast.makeText(Toast.java:231) 07-15 16:09:39.152:E / AndroidRuntime(29508):at com.dillonregi.spamsms.Main $ 1 $ 1.run(Main.java:37) 07-15 16:09:39.152:E / AndroidRuntime(29508):在android.os.Handler.handleCallback(Handler.java:587) 07-15 16:09:39.152:E / AndroidRuntime(29508):在android.os.Handler.dispatchMessage(Handler.java:92) 07-15 16:09:39.152:E / AndroidRuntime(29508):在android.os.Looper.loop(Looper.java:130) 07-15 16:09:39.152:E / AndroidRuntime(29508):在android.app.ActivityThread.main(ActivityThread.java:3701) 07-15 16:09:39.152:E / AndroidRuntime(29508):at java.lang.reflect.Method.invokeNative(Native Method) 07-15 16:09:39.152:E / AndroidRuntime(29508):at java.lang.reflect.Method.invoke(Method.java:507) 07-15 16:09:39.152:E / AndroidRuntime(29508):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:895) 07-15 16:09:39.152:E / AndroidRuntime(29508):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653) 07-15 16:09:39.152:E / AndroidRuntime(29508):at dalvik.system.NativeStart.main(Native Method) 07-15 16:09:39.162:E / AndroidRuntime(29508):[蓝色错误处理程序]为主调试报告文件 07-15 16:09:39.162:E / AndroidRuntime(29508):java.lang.NullPointerException 07-15 16:09:39.162:E / AndroidRuntime(29508):在android.widget.Toast。(Toast.java:89) 07-15 16:09:39.162:E / AndroidRuntime(29508):在android.widget.Toast.makeText(Toast.java:231) 07-15 16:09:39.162:E / AndroidRuntime(29508):at com.dillonregi.spamsms.Main $ 1 $ 1.run(Main.java:37) 07-15 16:09:39.162:E / AndroidRuntime(29508):在android.os.Handler.handleCallback(Handler.java:587) 07-15 16:09:39.162:E / AndroidRuntime(29508):在android.os.Handler.dispatchMessage(Handler.java:92) 07-15 16:09:39.162:E / AndroidRuntime(29508):在android.os.Looper.loop(Looper.java:130) 07-15 16:09:39.162:E / AndroidRuntime(29508):在android.app.ActivityThread.main(ActivityThread.java:3701) 07-15 16:09:39.162:E / AndroidRuntime(29508):at java.lang.reflect.Method.invokeNative(Native Method) 07-15 16:09:39.162:E / AndroidRuntime(29508):at java.lang.reflect.Method.invoke(Method.java:507) 07-15 16:09:39.162:E / AndroidRuntime(29508):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:895) 07-15 16:09:39.162:E / AndroidRuntime(29508):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653) 07-15 16:09:39.162:E / AndroidRuntime(29508):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
有道理吗?
Toast.makeText(null, "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
您正在通过null
而不是Context
通过
Toast.makeText(Main.this , "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
答案 1 :(得分:1)
您的Logcat显示此错误:
E/AndroidRuntime(29508): java.lang.NullPointerException 07-15 16:09:39.152:
E/AndroidRuntime(29508): at android.widget.Toast.(Toast.java:89) 07-15 16:09:39.152:
E/AndroidRuntime(29508): at android.widget.Toast.makeText(Toast.java:231) 07-15 16:09:39.152:
所以你的错误基本上是你传递了一个空指针给你的Toast小部件,编译器抱怨这个!!
onPostExecute()
方法中的用这个替换你的祝酒词:
将sentTexts
作为您班级的属性
Toast.makeText(Main.this, "number of texts sent will end up going here. maybe add" + sentTexts + "after these quotes?", Toast.LENGTH_LONG).show();
答案 2 :(得分:0)
您在第Toat.makeToast
行获取NullPointerException,您将null
作为第一个参数发送,其中应包含上下文。
所有迹象都指向同一个地方......