gpt4 book ai didi

android - sms.sendTextMessage 发送消息但强制关闭

转载 作者:行者123 更新时间:2023-11-29 22:02:58 26 4
gpt4 key购买 nike

编辑:解决方案是将 Main.this 放在 toast 中的 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: thread exiting with uncaught exception (group=0x40019560) 07-15 16:09:39.152: E/AndroidRuntime(29508): FATAL EXCEPTION: main 07-15 16:09:39.152: 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: E/AndroidRuntime(29508): at com.dillonregi.spamsms.Main$1$1.run(Main.java:37) 07-15 16:09:39.152: E/AndroidRuntime(29508): at android.os.Handler.handleCallback(Handler.java:587) 07-15 16:09:39.152: E/AndroidRuntime(29508): at android.os.Handler.dispatchMessage(Handler.java:92) 07-15 16:09:39.152: E/AndroidRuntime(29508): at android.os.Looper.loop(Looper.java:130) 07-15 16:09:39.152: E/AndroidRuntime(29508): at 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): [Blue Error Handler] Make Debugging Report file for main 07-15 16:09:39.162: E/AndroidRuntime(29508): java.lang.NullPointerException 07-15 16:09:39.162: E/AndroidRuntime(29508): at android.widget.Toast.(Toast.java:89) 07-15 16:09:39.162: E/AndroidRuntime(29508): at 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): at android.os.Handler.handleCallback(Handler.java:587) 07-15 16:09:39.162: E/AndroidRuntime(29508): at android.os.Handler.dispatchMessage(Handler.java:92) 07-15 16:09:39.162: E/AndroidRuntime(29508): at android.os.Looper.loop(Looper.java:130) 07-15 16:09:39.162: E/AndroidRuntime(29508): at 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)

最佳答案

有道理吗??

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();

关于android - sms.sendTextMessage 发送消息但强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11495260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com