0

Androidアプリを実行しているときに、強制終了の問題に直面しています。サーバーから受信した応答コードに従って「トースト」を作成しようとすると、強制終了エラーが発生します。メッセージの送信に問題はありませんが、メッセージは毎回適切に配信されているためです。トーストの表示中にエラーが発生します。すべてのコードとログメッセージを貼り付けています。エラーを整理するのを手伝ってください

ログ猫

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

}
4

2 に答える 2

1

そのスレッドからトーストを呼び出すことはできません。UIスレッドでコードを実行する必要があります。これはそれを回避する最も簡単な方法です:

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

    }
});

また、このコードは、この質問に対する私の解決策が正しかったことを意味しますか?その場合は、正しい答えを選択することを忘れないでください。そうでない場合は、同じコードを含む別の質問をする前に、その質問を解決してください。

編集:また、このトーストメッセージを呼び出す例外が発生する理由は、保存する前に応答コードを送信するためです。への最初の呼び出しのみがreadLine()応答コードを返します。あなたが送った最初の質問System.out.println()では、何もしませんでした。今、あなたはそれをあなたが見ることができるようにあなたのログに送っています、しかしそれからそれはその後消えました。ログに記録する前に保存する必要があります。

responsecode = br.readLine();
Log.d("SmsResponse", responsecode);
于 2012-09-05T12:48:26.640 に答える
0

スレッドは、他のUI操作が保持されるようになり、エラーが発生するため、メインUIスレッドでは実行できない重いタスクや時間のかかるタスクが必要な場合に使用されます。

そのため、ネットワーク接続のマッキングやスレッドが必要なファイルのダウンロードなど、時間のかかるタスクを実行します。

ただし、toastはメインUIスレッドでのみ実行されるUI要素です。

于 2014-09-30T09:04:49.477 に答える