gpt4 book ai didi

android - Toast 未显示并出错

转载 作者:太空狗 更新时间:2023-10-29 15:38:02 26 4
gpt4 key购买 nike

我想在互联网连接可用时显示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

最佳答案

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

关于android - Toast 未显示并出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31822988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com