我的应用程序启动正常,但按下发送按钮后,它会强行关闭。 Eclipse中没有“问题”...... 我认为这是导致错误的代码:
public void sms() {
Toast.makeText(this, "sms()", Toast.LENGTH_SHORT).show();
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);
Toast.makeText(null, "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
long totalSize = 0;
for (int i = 0; i < xtime; i++) {
SmsManager.getDefault().sendTextMessage(_number, null, _message, null, null);
totalSize += 1;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(Long result) {
Toast.makeText(null, "number of texts sent will end up going here. maybe add + totalSize after the quotes?", Toast.LENGTH_LONG).show();
}
};
}
这是LogCat:
07-14 19:32:39.742:W / dalvikvm(16712):threadid = 1:线程退出,未捕获异常(group = 0x40019560) 07-14 19:32:39.752:E / AndroidRuntime(16712):致命异常:主要 07-14 19:32:39.752:E / AndroidRuntime(16712):java.lang.IllegalStateException:无法在视图类android上的onClick处理程序的活动类com.dillonregi.spamsms.Main中找到方法sms(View)。 widget.Button,id为'psend' 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View $ 1.onClick(View.java:2143) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View.performClick(View.java:2497) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View $ PerformClick.run(View.java:9120) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.os.Handler.handleCallback(Handler.java:587) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.os.Handler.dispatchMessage(Handler.java:92) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.os.Looper.loop(Looper.java:130) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.app.ActivityThread.main(ActivityThread.java:3701) 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.reflect.Method.invokeNative(Native Method) 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.reflect.Method.invoke(Method.java:507) 07-14 19:32:39.752:E / AndroidRuntime(16712):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:895) 07-14 19:32:39.752:E / AndroidRuntime(16712):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653) 07-14 19:32:39.752:E / AndroidRuntime(16712):at dalvik.system.NativeStart.main(Native Method) 07-14 19:32:39.752:E / AndroidRuntime(16712):引起:java.lang.NoSuchMethodException:sms 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.ClassCache.findMethodByName(ClassCache.java:247) 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.Class.getMethod(Class.java:962) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View $ 1.onClick(View.java:2136) 07-14 19:32:39.752:E / AndroidRuntime(16712):... 11更多 07-14 19:32:39.752:E / AndroidRuntime(16712):[蓝色错误处理程序]为主要调试报告文件 07-14 19:32:39.752:E / AndroidRuntime(16712):java.lang.IllegalStateException:无法在视图类android上的onClick处理程序的活动类com.dillonregi.spamsms.Main中找到方法sms(View)。 widget.Button,id为'psend' 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View $ 1.onClick(View.java:2143) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View.performClick(View.java:2497) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View $ PerformClick.run(View.java:9120) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.os.Handler.handleCallback(Handler.java:587) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.os.Handler.dispatchMessage(Handler.java:92) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.os.Looper.loop(Looper.java:130) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.app.ActivityThread.main(ActivityThread.java:3701) 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.reflect.Method.invokeNative(Native Method) 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.reflect.Method.invoke(Method.java:507) 07-14 19:32:39.752:E / AndroidRuntime(16712):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:895) 07-14 19:32:39.752:E / AndroidRuntime(16712):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653) 07-14 19:32:39.752:E / AndroidRuntime(16712):at dalvik.system.NativeStart.main(Native Method) 07-14 19:32:39.752:E / AndroidRuntime(16712):引起:java.lang.NoSuchMethodException:sms 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.ClassCache.findMethodByName(ClassCache.java:247) 07-14 19:32:39.752:E / AndroidRuntime(16712):at java.lang.Class.getMethod(Class.java:962) 07-14 19:32:39.752:E / AndroidRuntime(16712):在android.view.View $ 1.onClick(View.java:2136) 07-14 19:32:39.752:E / AndroidRuntime(16712):... 11更多
答案 0 :(得分:0)
你的短信方法的方法签名是错误的。如果它在布局文件中分配,则应采用View参数:
public void sms(View view) {
// code to run when button is clicked
}
答案 1 :(得分:0)
首先,您应该更新您的问题以格式化您的代码 其次,您尝试在AsycnTask的doInBackground()中触摸UI线程。不要这样做:
Toast.makeText(null, "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
第三,尝试声明这样的短信方法:
public void sms(View view) {
//your code goes here
}
最后,您创建了新的AsyncTask,但我没有看到您调用excute()方法。尝试调用excute()来执行。
<强>更新强>: 您可以使用Handler制作Toast或在UI线程中执行某些操作。
//put this Handler in your activity, out site the AsyncTask.(**important**)
final Handler mHandler = new Handler();
//Then call it in doInBackground() of AsyncTask to show a Toast.
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);
//Show a Toast
mHandler.post(new Runnable() {
public void run() {
Toast.makeText(null, "number: " + _number + " message: " + _message + " xtime: " + xtime, Toast.LENGTH_LONG).show();
}
);
}