gpt4 book ai didi

Android 应用程序在启动时崩溃

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

我正在开发一个连接到我的 PC 服务器的客户端应用程序,但在运行时我的应用程序崩溃了。我认为问题出在我的 androidmanifest.xml 中的互联网权限

-->安卓应用程序代码:

public class Main extends Activity {

Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b = (Button)findViewById(R.id.connect); //reference to the send button
b.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View arg0) {

try {
Socket client = new Socket("192.168.1.2", 4444); //connect to server
client.close(); //closing the connection

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

}

});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

-->AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.client"
android:versionCode="1"
android:versionName="1.0" >



<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.client.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

这是我的 LogCat:

    05-29 13:32:22.850: E/AndroidRuntime(18707): FATAL EXCEPTION: main
05-29 13:32:22.850: E/AndroidRuntime(18707): android.os.NetworkOnMainThreadException
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
05-29 13:32:22.850: E/AndroidRuntime(18707): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-29 13:32:22.850: E/AndroidRuntime(18707): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-29 13:32:22.850: E/AndroidRuntime(18707): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.startupSocket(Socket.java:566)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.tryAllAddresses(Socket.java:127)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.<init>(Socket.java:177)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.<init>(Socket.java:149)
05-29 13:32:22.850: E/AndroidRuntime(18707): at com.example.client.Main$1.onClick(Main.java:39)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.view.View.performClick(View.java:4240)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.view.View$PerformClick.run(View.java:17721)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.Handler.handleCallback(Handler.java:730)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.Looper.loop(Looper.java:137)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.lang.reflect.Method.invoke(Method.java:525)
05-29 13:32:22.850: E/AndroidRuntime(18707): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-29 13:32:22.850: E/AndroidRuntime(18707): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-29 13:32:22.850: E/AndroidRuntime(18707): at dalvik.system.NativeStart.main(Native Method)

请帮帮我

最佳答案

移动

 Socket client = new Socket("192.168.1.2", 4444);  //connect to server
client.close(); //closing the connection

进入AsyncTaskThread

如果您在 UIThread 上执行 Network 操作,则会抛出 NetworkOnMainThreadException

关于Android 应用程序在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23932287/

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