简体   繁体   中英

java.lang.InstantiationException

I am making an app to send X amount of texts to people and after re-writing my code to include threads, it broke. Also, will the "Thread.sleep(3000);" portion of the code freeze the UI, or will having all my code in a thread fix that issue? Final question, how can i prevent the script from stopping when in the background (to continue sending)

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

thanks, and here is the LogCat

07-12 21:04:40.222: E/AndroidRuntime(13948): FATAL EXCEPTION: main 07-12 21:04:40.222: E/AndroidRuntime(13948): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dillonregi.multisender/com.dillonregi.multisender.multisender2}: java.lang.InstantiationException: com.dillonregi.multisender.multisender2 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1580) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.os.Handler.dispatchMessage(Handler.java:99) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.os.Looper.loop(Looper.java:130) 07-12 21:04 :40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.main(ActivityThread.java:3701) 07-12 21:04:40.222: E/AndroidRuntime(13948): at java.lang.reflect.Method.invokeNative(Native Method) 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): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 07-12 21:04:40.222: E/AndroidRuntime(13948): at 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): Caused by: java.lang.InstantiationException: com.dillonregi.multisender.multisender2 07-12 21:04:40.222: E/AndroidRuntime(13948): at 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): at android.ap p.Instrumentation.newActivity(Instrumentation.java:1021) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1572) 07-12 21:04:40.222: E/AndroidRuntime(13948): ... 11 more 07-12 21:04:40.222: E/AndroidRuntime(13948): [Blue Error Handler] Make Debugging Report file for main 07-12 21:04:40.222: E/AndroidRuntime(13948): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dillonregi.multisender/com.dillonregi.multisender.multisender2}: java.lang.InstantiationException: com.dillonregi.multisender.multisender2 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1580) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 07-12 21:04:40.222: E/AndroidRuntime(13948) : at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.os.Handler.dispatchMessage(Handler.java:99) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.os.Looper.loop(Looper.java:130) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.main(ActivityThread.java:3701) 07-12 21:04:40.222: E/AndroidRuntime(13948): at java.lang.reflect.Method.invokeNative(Native Method) 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): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 07-12 21:04:40.222: E/AndroidRuntime(13948): at 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): Caused by: java.lang.InstantiationException: com.dillonregi .multisender.multisender2 07-12 21:04:40.222: E/AndroidRuntime(13948): at 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): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 07-12 21:04:40.222: E/AndroidRuntime(13948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1572) 07-12 21:04:40.222: E/AndroidRuntime(13948): ... 11 more

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM