gpt4 book ai didi

java - 我的套接字应用程序停止工作 - 如何修复它?

转载 作者:行者123 更新时间:2023-12-03 17:04:07 26 4
gpt4 key购买 nike

我正在尝试创建一个 android 应用程序,它将通过套接字发送一个字符串,但是该应用程序在我的 android 上启动后立即停止,我的主要 Activity 代码是:

package com.example.phoenix.coreclient;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings.System;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

class AsyncTaskActivity extends Activity implements OnClickListener {

Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button_id);
// because we implement OnClickListener we only have to pass "this"
// (much easier)
btn.setOnClickListener(this);
}

public void onClick(View view) {
// detect the view that was "clicked"
switch (view.getId()) {
case (R.id.button_id):
new LongOperation().execute("");
break;
}
}

private class LongOperation extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}

@Override
protected void onPostExecute(String result) {
final TextView mStatusText = (TextView) findViewById(R.id.text_view_id);

try {
Socket socket = new Socket("192.168.1.68", 60000);
OutputStream out = socket.getOutputStream();
PrintWriter output = new PrintWriter(out);

mStatusText.setText("Sending Data to PC");
output.println("Hello from Android");
out.flush();
out.close();
mStatusText.setText("Data sent to PC");

socket.close();
mStatusText.setText("Socket closed");
} catch (Exception e) {
mStatusText.setText(e.toString());
}
}

@Override
protected void onPreExecute() {
}

@Override
protected void onProgressUpdate(Void... values) {
}
}
}

我不太擅长调试我需要一些帮助
09-15 21:01:38.837 29818-29818/? D/dalvikvm: Late-enabling CheckJNI
09-15 21:01:38.951 29818-29818/com.example.phoenix.coreclient D/HyLog: I : /data/font/config/dfactpre.dat, No such file or directory (2)
09-15 21:01:39.061 29818-29818/com.example.phoenix.coreclient D/AndroidRuntime: Shutting down VM
09-15 21:01:39.061 29818-29818/com.example.phoenix.coreclient W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419a8d58)
09-15 21:01:39.080 29818-29818/com.example.phoenix.coreclient E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.phoenix.coreclient, PID: 29818
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.phoenix.coreclient/com.example.phoenix.coreclient.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.phoenix.coreclient.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.phoenix.coreclient-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.phoenix.coreclient-2, /vendor/lib, /system/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.phoenix.coreclient.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.phoenix.coreclient-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.phoenix.coreclient-2, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2119)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252) 
at android.app.ActivityThread.access$800(ActivityThread.java:139) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
at dalvik.system.NativeStart.main(Native Method) 

那么这里的问题是什么,我该如何解决呢?我已经添加了互联网权限,服务器端是经过测试的计算机上的简单 python 代码,它工作得很好

最佳答案

您没有将 Activity 添加到 AndroidManifest.xml。
您应该在 list 文件中注册您的 Activity 。

它应该是这样的:

<application
android:name=".Phoenix"
android:icon="@mipmap/ic_launcher"
...>
<activity
android:name=".AsyncTaskActivity"
...>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
</application>

关于java - 我的套接字应用程序停止工作 - 如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52347500/

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