Reputation: 439
Edit: the solution was to put Main.this in place of null in my toast.
i am using AsyncTask to send text messages and they all get sent successfully (i have tested sending 50 to myself) but the application instantly force closes when i press send. i used breakpoints and i decided that the application force closes after the "sms.sendTextMessage..." loop. Here is my 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(); }
}
And the 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)
Upvotes: 1
Views: 523
Reputation: 31456
your Logcat show this error:
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:
So your error is basically you passed a null pointer to your Toast widget and compiler complain about this!!
in onPostExecute()
method replace your toast with this one:
make sentTexts
as attribute of you class
Toast.makeText(Main.this, "number of texts sent will end up going here. maybe add" + sentTexts + "after these quotes?", Toast.LENGTH_LONG).show();
Upvotes: 1
Reputation: 4157
You are getting a NullPointerException at the line Toat.makeToast
where you send null
as the first parameter, where the context should be.
All the signs point to the same place...
Upvotes: 0
Reputation: 12642
it make sense??
Toast.makeText(null, "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
you are passing null
instead of Context
change it with
Toast.makeText(Main.this , "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
Upvotes: 1