2

我正在制作一个应用程序来向人们发送 X 数量的文本,并且在重写我的代码以包含线程之后,它坏了。另外,将“Thread.sleep(3000);” 部分代码冻结了 UI,或者将我的所有代码放在一个线程中解决该问题?最后一个问题,如何防止脚本在后台停止(继续发送)

public abstract class multisender2 extends Activity implements Runnable {
    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
    public void csend(View view) {
     EditText number = (EditText)findViewById(R.id.pnumber);
     EditText message = (EditText)findViewById(R.id.pmessage);
     EditText hmany = (EditText)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);
     final int count = 1;
    Thread background = new Thread(new Runnable() { 

@Override
public void run() {
    try {
    if (count > xtime == false){
    SmsManager.getDefault().sendTextMessage(_number, null, _message, null, null);
        Thread.sleep(3000);
              }
    } catch (InterruptedException e) {
 }
 }

});       
    background.start();
}
}

谢谢,这里是 LogCat

07-12 21:04:40.222: E/AndroidRuntime(13948): 致命异常: main 07-12 21:04:40.222: E/AndroidRuntime(13948): java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com. dillonregi.multisender/com.dillonregi.multisender.multisender2}:java.lang.InstantiationException:com.dillonregi.multisender.multisender2 07-12 21:04:40.222:E/AndroidRuntime(13948):在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1580) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674) 07-12 21:04:40.222: E/AndroidRuntime (13948): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java :942) 07-12 21:04:40.222:E/AndroidRuntime(13948): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.os.Looper.loop(Looper.java :130) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.app.ActivityThread.main(ActivityThread.java:3701) 07-12 21:04:40.222: E/AndroidRuntime(13948):在 java.lang.reflect.Method.invokeNative(Native Method) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 java.lang.reflect.Method.invoke(Method.java:507) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 07-12 21:04:40.222: E/AndroidRuntime(13948):在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 dalvik.system.NativeStart.main(Native Method) 07-12 21:04:40。222: E/AndroidRuntime(13948): 引起: java.lang.InstantiationException: com.dillonregi.multisender.multisender2 07-12 21:04:40.222: E/AndroidRuntime(13948): at java.lang.Class.newInstanceImpl(本机方法) 07-12 21:04:40.222: E/AndroidRuntime(13948): at java.lang.Class.newInstance(Class.java:1409) 07-12 21:04:40.222: E/AndroidRuntime(13948):在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1572) 07-12 21 :04:40.222: E/AndroidRuntime(13948): ... 11 更多 07-12 21:04:40.222: E/AndroidRuntime(13948): [蓝色错误处理程序] 为 main 07-12 21:04 制作调试报告文件:40.222: E/AndroidRuntime(13948): java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com.dillonregi.multisender/com.dillonregi.multisender.multisender2}:java.lang.InstantiationException:com.dillonregi.multisender.multisender2 07-12 21:04:40.222:E/AndroidRuntime(13948):在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1580) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674) 07-12 21:04:40.222: E/AndroidRuntime (13948): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java :942) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-12 21:04:40.222: E/AndroidRuntime(13948):在 android.os.Looper.loop(Looper.java:130) 07-12 21:04:40.222:E/AndroidRuntime(13948): 在 android.app.ActivityThread.main(ActivityThread.java:3701) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 java.lang.reflect.Method.invokeNative(Native方法) 07-12 21:04:40.222: E/AndroidRuntime(13948): at java.lang.reflect.Method.invoke(Method.java:507) 07-12 21:04:40.222: E/AndroidRuntime(13948) : 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 com.android.internal.os.ZygoteInit.main (ZygoteInit.java:653) 07-12 21:04:40.222: E/AndroidRuntime(13948): at dalvik.system.NativeStart.main(Native Method) 07-12 21:04:40.222: E/AndroidRuntime(13948) :引起:java.lang.InstantiationException:com.dillonregi.multisender.multisender2 07-12 21:04:40.222:E/AndroidRuntime(13948):在 java.lang.Class。newInstanceImpl(Native Method) 07-12 21:04:40.222: E/AndroidRuntime(13948): at java.lang.Class.newInstance(Class.java:1409) 07-12 21:04:40.222: E/AndroidRuntime(13948) ): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 07-12 21:04:40.222: E/AndroidRuntime(13948): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1572) 07- 12 21:04:40.222: E/AndroidRuntime(13948): ... 11 更多还有 11 个还有 11 个

4

1 回答 1

0

Do not block the main thread. Try to use AsyncTask. Fore more information read - Processes and Threads

于 2012-07-13T01:13:06.070 回答