当我执行我的Android应用程序时,我正面临一个强制关闭问题。当我尝试根据从服务器收到的响应代码制作“Toast”时,我面临强制关闭错误。即使发送消息没有问题,因为每次都会正确传递消息。显示吐司时发生错误。我粘贴所有代码和日志消息。请帮我解决错误
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);
}
}
答案 0 :(得分:1)
You cannot call a Toast from that thread.您需要在UI线程上运行代码。这是解决问题的最简单方法:
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "SMS Sending Failed.",
Toast.LENGTH_SHORT).show();
}
});
此代码也意味着我对this question的解决方案是正确的吗?如果是,请remember to select the correct answer,如果没有,请在提出涉及相同代码的单独问题之前解决该问题。
编辑:此外,您收到调用此Toast消息的异常的原因是您在存储之前发送了响应代码。只有第一次调用readLine()
才会返回响应代码。在您发送给System.out.println()
的第一个问题中,它没有做任何事情。现在您将它发送到您的日志,以便您能够看到它,但之后它就消失了。您需要在记录之前存储它:
responsecode = br.readLine();
Log.d("SmsResponse", responsecode);
答案 1 :(得分:0)
当我们需要执行繁重的任务或耗时的任务而无法在主UI线程上执行线程时使用线程,因为其他UI操作将会保持并且它会给您和错误。
这样做耗时的任务,如Makking网络连接或下载我们需要线程的任何文件。
但toast是一个只在主UI线程上执行的UI元素。