我希望在互联网连接可用时显示Toast
但是会出错。
我的代码
public class MainActivity extends ActionBarActivity {
EditText et, pass;
ConnectivityManager cm;
NetworkInfo ni;
ProgressDialog dialog = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("LOG", "Start <-- ");
et = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
}
public void onClick(View v) {
Log.i("LOG", "click button <-- ");
if(!et.getText().toString().matches("") &&
!pass.getText().toString().matches(""))
{
Log.i("LOG", "check network <-- ");
isNetworkConnected();
}else{
Toast.makeText(getBaseContext(),"Enter user & pass",
Toast.LENGTH_SHORT).show();
}}
private boolean isNetworkConnected() {
Log.i("LOG", "start check network <-- ");
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
ni = cm.getActiveNetworkInfo();
if (ni == null) {
Toast.makeText(getApplicationContext(),
"Make sure you connect to the Internet!", Toast.LENGTH_SHORT).show();
return false;
}else {
waiting();
return true;
}
}
void waiting(){
dialog = ProgressDialog.show(MainActivity.this, "",
"Please wait a moment....", true);
new Thread(new Runnable() {
public void run() {
ShowToast(MainActivity.this);
}
}).start();
}
void ShowToast(Context context) {
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
Log.i("LOG", "1 <-- ");
Toast.makeText(context,"Connect OK",
Toast.LENGTH_SHORT).show();
Log.i("LOG", "2 <-- ");
}
}
这是日志中的错误
08-04 23:24:45.476: I/LOG(1169): 1 <--
08-04 23:24:45.476: W/dalvikvm(1169): threadid=14: thread exiting with uncaught exception (group=0x414c4700)
08-04 23:24:45.506: E/AndroidRuntime(1169): FATAL EXCEPTION: Thread-99
08-04 23:24:45.506: E/AndroidRuntime(1169): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-04 23:24:45.506: E/AndroidRuntime(1169): at android.os.Handler.<init>(Handler.java:197)
08-04 23:24:45.506: E/AndroidRuntime(1169): at android.os.Handler.<init>(Handler.java:111)
08-04 23:24:45.506: E/AndroidRuntime(1169): at android.widget.Toast$TN.<init>(Toast.java:324)
08-04 23:24:45.506: E/AndroidRuntime(1169): at android.widget.Toast.<init>(Toast.java:91)
08-04 23:24:45.506: E/AndroidRuntime(1169): at android.widget.Toast.makeText(Toast.java:238)
08-04 23:24:45.506: E/AndroidRuntime(1169): at ir.sheikhoo.registerproject.MainActivity.ShowToast(MainActivity.java:101)
08-04 23:24:45.506: E/AndroidRuntime(1169): at ir.sheikhoo.registerproject.MainActivity$1.run(MainActivity.java:87)
08-04 23:24:45.506: E/AndroidRuntime(1169): at java.lang.Thread.run(Thread.java:841)
08-04 23:24:45.577: W/ActivityManager(286): Force finishing activity ir.sheikhoo.registerproject/.MainActivity
08-04 23:24:45.596: W/WindowManager(286): Screenshot failure taking screenshot for (164x246) to layer 21015
答案 0 :(得分:4)
Toast应该在UI线程上,
替换,
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
}
});
Log.i("LOG", "1 <-- ");
Toast.makeText(context,"Connect OK",
Toast.LENGTH_SHORT).show();
Log.i("LOG", "2 <-- ");
with
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
Toast.makeText(context,"Connect OK",
Toast.LENGTH_SHORT).show();
}
});
Log.i("LOG", "1 <-- ");
Log.i("LOG", "2 <-- ");
答案 1 :(得分:3)
根据错误,您正在从工作线程调用toast,但是您应该从处理UI线程的函数中调用它。 在这里检查同样的问题: Can't create handler inside thread that has not called Looper.prepare()
答案 2 :(得分:3)
您只能在UI线程中显示Toast
。不确定为什么要在那里创建一个新线程。
void waiting(){
dialog = ProgressDialog.show(MainActivity.this, "",
"Please wait a moment....", true);
new Thread(new Runnable() {
public void run() {
ShowToast(MainActivity.this);
}
}).start();
}
void ShowToast(Context context) {
runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
Log.i("LOG", "1 <-- ");
Toast.makeText(context,"Connect OK",
Toast.LENGTH_SHORT).show();
Log.i("LOG", "2 <-- ");
}
});
}
答案 3 :(得分:1)
您可以通过调用ShowToast(context)
使用handler来处理handleMessage或者您可以像ShowToast一样调用ShowToast(getApplicationContext());