gpt4 book ai didi

Android Toast 崩溃

转载 作者:行者123 更新时间:2023-12-02 08:11:01 25 4
gpt4 key购买 nike

我在我的 Android 应用程序中创建了一个连接到 PC 服务器的客户端。当我的应用程序连接到服务器时,我想看到“已建立连接”,为此我正在使用 toast ,但我的程序在启动时崩溃。这是代码:

 protected String doInBackground(String... params) {

try {
Socket client = new Socket("192.168.1.2", 4444); //connect to server
Toast t = Toast.makeText(getApplicationContext(), "Connection established" ,Toast.LENGTH_LONG);
t.show();
client.close(); //closing the connection

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}

return null;
}

自从我写了“Toast t = Toast.makeText(getApplicationContext(),“连接已建立”,Toast.LENGTH_LONG);”以来,我就遇到了这个问题

这是日志猫:

    05-29 16:28:54.076: E/AndroidRuntime(32436): FATAL EXCEPTION: AsyncTask #1
05-29 16:28:54.076: E/AndroidRuntime(32436): java.lang.RuntimeException: An error occured while executing doInBackground()
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-29 16:28:54.076: E/AndroidRuntime(32436): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-29 16:28:54.076: E/AndroidRuntime(32436): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-29 16:28:54.076: E/AndroidRuntime(32436): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-29 16:28:54.076: E/AndroidRuntime(32436): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-29 16:28:54.076: E/AndroidRuntime(32436): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-29 16:28:54.076: E/AndroidRuntime(32436): at java.lang.Thread.run(Thread.java:841)
05-29 16:28:54.076: E/AndroidRuntime(32436): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.os.Handler.<init>(Handler.java:197)
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.os.Handler.<init>(Handler.java:111)
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.widget.Toast$TN.<init>(Toast.java:324)
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.widget.Toast.<init>(Toast.java:91)
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.widget.Toast.makeText(Toast.java:238)
05-29 16:28:54.076: E/AndroidRuntime(32436): at com.example.social_network.MainActivity$myTask.doInBackground(MainActivity.java:34)
05-29 16:28:54.076: E/AndroidRuntime(32436): at com.example.social_network.MainActivity$myTask.doInBackground(MainActivity.java:1)
05-29 16:28:54.076: E/AndroidRuntime(32436): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-29 16:28:54.076: E/AndroidRuntime(32436): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-29 16:28:54.076: E/AndroidRuntime(32436): ... 4 more

谢谢

最佳答案

当然,您需要使用 AsyncTask 的最佳实践。例如检查 this post .

但是如果您只想快速测试它,请尝试以下操作:

替换这些行:

Toast t = Toast.makeText(getApplicationContext(), "Connection established", Toast.LENGTH_LONG);
t.show();

由这些人:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
Toast t = Toast.makeText(getApplicationContext(), "Connection established", Toast.LENGTH_LONG);
t.show();
}
});

更新代码:

替换这一行:

if(result.equalsIgnoreCase("Exception Caught")){

通过这个:

if ("Exception Caught".equalsIgnoreCase(result)) {

关于Android Toast 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23935927/

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