简体   繁体   中英

threadid=9: thread exiting with uncaught exception (group=0x40015560)

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

}

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

Threads are used when we have to a heavy task or time consuming task which cannot be carried on the Main UI thread as the other UI Operation's will come to hold and it'll give you and error.

So doing such time consuming task such as Makking Network connection or downloading any file we need Threads.

But toast is a UI element which will only execute on Main UI thread.

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