Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8486801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:03:25+00:00 2026-06-10T21:03:25+00:00

I am facing a force close problem when i am executing my android app.

  • 0

I am facing a force close problem when i am executing my android app. I am facing force close error when i am trying to make a “Toast” as per the response code received from server. Even though there is no problem is sending the messages, because the message is getting delivered properly everytime. The error occurs while displaying the toast. I am pasting all the codes and log messages. Please help me in sorting out the error

Log Cat

09-05 10:08:02.122: W/KeyCharacterMap(719): No keyboard for id 0
09-05 10:08:02.122: W/KeyCharacterMap(719): Using default keymap: /system/usr/keychars 
/qwerty.kcm.bin
09-05 10:08:30.392: D/dalvikvm(719): GC_EXTERNAL_ALLOC freed 228K, 53% free 2625K/5511K,   
external 2061K/2137K, paused 64ms
09-05 10:08:34.382: D/SmsResponse(719): 1
09-05 10:08:34.382: W/dalvikvm(719): threadid=9: thread exiting with uncaught exception  
(group=0x40015560)
09-05 10:08:34.412: E/AndroidRuntime(719): FATAL EXCEPTION: Thread-10
09-05 10:08:34.412: E/AndroidRuntime(719): java.lang.RuntimeException: Can't create handler  
inside thread that has not called Looper.prepare()
09-05 10:08:34.412: E/AndroidRuntime(719):  at android.os.Handler.<init>(Handler.java:121)
09-05 10:08:34.412: E/AndroidRuntime(719):  at android.widget.Toast.<init>(Toast.java:68)
09-05 10:08:34.412: E/AndroidRuntime(719):  at android.widget.Toast.makeText(Toast.java:231)
09-05 10:08:34.412: E/AndroidRuntime(719):  at 
com.bluetitan.freesms.MainActivity.sendMessage(MainActivity.java:65)
09-05 10:08:34.412: E/AndroidRuntime(719):  at 
com.bluetitan.freesms.MainActivity.run(MainActivity.java:144)

09-05 10:08:34.412: E/AndroidRuntime(719):  at java.lang.Thread.run(Thread.java:1019)
09-05 10:08:34.742: W/IInputConnectionWrapper(719): showStatusIcon on inactive InputConnection
09-05 10:08:36.142: E/WindowManager(719): Activity com.bluetitan.freesms.MainActivity has leaked 
window com.android.internal.policy.impl.PhoneWindow$DecorView@40558428 that was originally added 
here
09-05 10:08:36.142: E/WindowManager(719): android.view.WindowLeaked: Activity   
com.bluetitan.freesms.MainActivity has leaked window  
com.android.internal.policy.impl.PhoneWindow$DecorView@40558428 that was originally added here
09-05 10:08:36.142: E/WindowManager(719):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
09-05 10:08:36.142: E/WindowManager(719):   at 
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
09-05 10:08:36.142: E/WindowManager(719):   at 
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
09-05 10:08:36.142: E/WindowManager(719):   at   
android.view.Window$LocalWindowManager.addView(Window.java:424)
09-05 10:08:36.142: E/WindowManager(719):   at android.app.Dialog.show(Dialog.java:241)
09-05 10:08:36.142: E/WindowManager(719):   at 
android.app.ProgressDialog.show(ProgressDialog.java:107)
09-05 10:08:36.142: E/WindowManager(719):   at 
android.app.ProgressDialog.show(ProgressDialog.java:90)

 09-05 10:08:36.142: E/WindowManager(719):  at 
 com.bluetitan.freesms.MainActivity.onClick(MainActivity.java:123)

 09-05 10:08:36.142: E/WindowManager(719):  at android.view.View.performClick(View.java:2485)

 09-05 10:08:36.142: E/WindowManager(719):  at android.view.View$PerformClick.run(View.java:9080)
 09-05 10:08:36.142: E/WindowManager(719):  at 
 android.os.Handler.handleCallback(Handler.java:587)
 09-05 10:08:36.142: E/WindowManager(719):  at 
 android.os.Handler.dispatchMessage(Handler.java:92)
 09-05 10:08:36.142: E/WindowManager(719):  at android.os.Looper.loop(Looper.java:123)
 09-05 10:08:36.142: E/WindowManager(719):  at   
 android.app.ActivityThread.main(ActivityThread.java:3683)
 09-05 10:08:36.142: E/WindowManager(719):  at java.lang.reflect.Method.invokeNative(Native 
 Method)
 09-05 10:08:36.142: E/WindowManager(719):  at java.lang.reflect.Method.invoke(Method.java:507)
 09-05 10:08:36.142: E/WindowManager(719):  at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)

 09-05 10:08:36.142: E/WindowManager(719):  at 
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 09-05 10:08:36.142: E/WindowManager(719):  at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

public class MainActivity extends Activity implements OnClickListener, Runnable {
Context context;

// public final static String EXTRA_NUMBER =
// "com.example.myfirstapp.NUMBER";
// public final static String EXTRA_MESSAGE =
// "com.example.myfirstapp.MESSAGE";
EditText editTextNum, editText, editUserName, editPassword;
Button btnsend;
ProgressDialog pd;
String gateway_name;
Thread t;
Spinner spinner1;
String toastMsg = "Message Sent To Server";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    editUserName = (EditText) findViewById(R.id.edit_userName);
    editPassword = (EditText) findViewById(R.id.edit_password);
    editTextNum = (EditText) findViewById(R.id.edit_number);
    editText = (EditText) findViewById(R.id.edit_message);
    spinner1 = (Spinner) findViewById(R.id.SpinnerGateway);
    btnsend = (Button) findViewById(R.id.btnsend);
    btnsend.setOnClickListener(this);
}

/** Called when the user clicks the Send button */
public void sendMessage() {
    String usrname = editUserName.getText().toString();
    String usrPassword = editPassword.getText().toString();
    String number = editTextNum.getText().toString();
    String message = editText.getText().toString();
    gateway_name = String.valueOf(spinner1.getSelectedItem());
    String msgreciever = number;
    String testMessage = message;
    try {
        SmsSender.sendMessage(msgreciever, testMessage, usrname,
                usrPassword, gateway_name);
        toastMsg = SmsSender.toastText;
    } catch (Exception ex) {
        Toast.makeText(MainActivity.this, "SMS Sending Failed.",
                Toast.LENGTH_SHORT).show();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.menu_settings:
        settingmenuClicked();
        return true;
    case R.id.menu_help:
        showHelp();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

public boolean isValid() {
    if (editUserName.getText().length() == 10
            && editPassword.getText().length() != 0
            && editTextNum.getText().length() == 10
            && editText.getText().length() != 0) {
        return true;
    }
    return false;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v == btnsend) {
        if (!isOnline()) {
            Toast.makeText(MainActivity.this,
                    "No Internet Access..Cannot Send SMS",
                    Toast.LENGTH_SHORT).show();
        } else if (!isValid()) {
            Toast.makeText(MainActivity.this,
                    "All fields are required. Try Again.",
                    Toast.LENGTH_SHORT).show();
        } else {
            pd = ProgressDialog.show(MainActivity.this, "Free Sms",
                    "Sending SMS..Please Wait..!!", true);
            t = new Thread(this);
            t.start();
        }

    }
}

public void settingmenuClicked() {
    Toast.makeText(MainActivity.this, "Setting Menu Coming Soon",
            Toast.LENGTH_SHORT).show();
}

public void showHelp() {
    Toast.makeText(MainActivity.this, "Help Coming Soon",
            Toast.LENGTH_SHORT).show();
}

public void run() {
    // TODO Auto-generated method stub
    sendMessage();
    mHandler.sendEmptyMessage(0);
}

public Handler mHandler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);
        pd.dismiss();
        Toast.makeText(MainActivity.this, "Message Sent to server", Toast.LENGTH_SHORT)
                .show();
        editTextNum.setText("");
        editText.setText("");
        editTextNum.requestFocus();
    }
};
}

SmsSender.java

public class SmsSender {


static final String _url = "http://ubaid.tk/sms/sms.aspx";
static final String charset = "UTF-8";
public static String responsecode = "0";
public static Integer responseInt;
public static String toastText = "Message Sent To Server";

// to build the query string that will send the message
private static String buildRequestString(String targetPhoneNo,
        String message, String userName, String Password, String Gateway)
        throws UnsupportedEncodingException {
    String[] params = new String[5];
    params[0] = userName;
    params[1] = Password;
    params[2] = message;
    params[3] = targetPhoneNo;
    params[4] = Gateway;

    String query = String.format(
            "uid=%s&pwd=%s&msg=%s&phone=%s&provider=%s",
            URLEncoder.encode(params[0], charset),
            URLEncoder.encode(params[1], charset),
            URLEncoder.encode(params[2], charset),
            URLEncoder.encode(params[3], charset),
            URLEncoder.encode(params[4], charset));
    return query;

}

public static void sendMessage(String reciever, String message,
        String userName, String password, String Gateway) throws Exception {
    // To establish the connection and perform the post request
    URLConnection connection = new URL(_url
            + "?"
            + buildRequestString(reciever, message, userName, password,
                    Gateway)).openConnection();
    connection.setRequestProperty("Accept-Charset", charset);
    // This automatically fires the request and we can use it to determine
    // the response status
    InputStream response = connection.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(response));
    // System.out.println(br.readLine());
    // response code = br.readLine();
    Log.d("SmsResponse", br.readLine());
    responsecode = br.readLine();
    responseInt = Integer.valueOf(responsecode).intValue();
    switch (responseInt) {
    case 1:
        toastText = "Message Sent Successfully";
        break;
    case -1:
        toastText = "Server Error";
        break;
    case -2:
        toastText = "Invalid Username";
        break;
    case -3:
        toastText = "Invalid Message Text";
        break;
    case -4:
        toastText = "Login Failed";
        break;
    case -5:
        toastText = "IP is blocked";
        break;
    default:
        toastText = "Unknown Error";
        break;
    }

    // responseInt = Integer.valueOf(br.readLine()).intValue();

}

public static void main(String[] args) throws Exception {
    // To DO
    // String testPhoneNo = "9876543210";
    // String testMessage = "Sending Messages From java is not too hard";
    // sendMessage(testPhoneNo, testMessage);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:03:26+00:00Added an answer on June 10, 2026 at 9:03 pm

    You cannot call a Toast from that thread. You need to run the code on the UI thread. This is the simplest way to get around it:

    this.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(MainActivity.this, "SMS Sending Failed.",
                Toast.LENGTH_SHORT).show();
    
        }
    });
    

    Also does this code mean that my solution to this question was correct? If so please remember to select the correct answer, If not please resolve that question before asking a separate question involving the same code.

    EDIT: Also the reason you are getting an exception that is calling this toast message is because you send the response code off before you store it. Only the first call to readLine() will return the response code. In the first question you sent to System.out.println() which did nothing. Now you are sending it to your log so you are able to see it, but then it’s gone after that. You need to store it before you log it:

    responsecode = br.readLine();
    Log.d("SmsResponse", responsecode);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am facing a problem in Message dialog, getting Force close my code
I am developing a android app, i am facing force close issue when i
Iam facing problem in understanding and converting a matlab code into opencv. I want
iam facing problem in passing array to view. this is my controller code.. function
I am facing this problem that buttons in android are not clickable when given
I am facing a strange problem. My app relies on a double conversion: currency
I'm facing a weird problem with my app: When pushing the menu button, my
I'm currently trying to code an app to send mass SMS to 300+ numbers
Facing problem with every fetch i want to make using EF Query model.Place here
I'm trying to copy android mmssms.db file to the SD card. And I'm facing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.