java - 设置 tcp 服务器时出现 NoClassDefFound 错误

标签 java android noclassdeffounderror tcpserver

我正在创建一个与 C# 通信的 tcp 服务器,并且我从此 website 访问了编码。我已经下载了源代码并添加了源代码中的外部 jar。每次我尝试在我的应用程序上运行服务器代码时,我都会在 LogCat 中收到 NoClassDefFound 。有人告诉我这可能与我的构建路径有关,这就是我提到外部 jar 的原因。我已经包含了 LogCat、Manifest 和 Java。感谢您的帮助。

08-06 13:31:54.989: W/dalvikvm(5164): threadid=1: thread exiting with uncaught exception (group=0x401f3760)
08-06 13:31:54.989: E/AndroidRuntime(5164): FATAL EXCEPTION: main
08-06 13:31:54.989: E/AndroidRuntime(5164): java.lang.NoClassDefFoundError: com.example.com.proto1.AndroidNetCommunicationClientActivityInner$1
08-06 13:31:54.989: E/AndroidRuntime(5164):     at com.example.com.proto1.AndroidNetCommunicationClientActivityInner.<init>(AndroidNetCommunicationClientActivityInner.java:102)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at java.lang.Class.newInstanceImpl(Native Method)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at java.lang.Class.newInstance(Class.java:1301)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1733)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.app.ActivityThread.access$500(ActivityThread.java:122)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.os.Looper.loop(Looper.java:132)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at android.app.ActivityThread.main(ActivityThread.java:4126)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at java.lang.reflect.Method.invoke(Method.java:491)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
08-06 13:31:54.989: E/AndroidRuntime(5164):     at dalvik.system.NativeStart.main(Native Method)

:31:54.989:E/AndroidRuntime(5164):在dalvik.system.NativeStart.main( native 方法)

Java

    package com.example.com.proto1;

import eneter.messaging.diagnostic.EneterTrace;
import eneter.messaging.endpoints.typedmessages.*;
import eneter.messaging.messagingsystems.messagingsystembase.*;
import eneter.messaging.messagingsystems.tcpmessagingsystem.TcpMessagingSystemFactory;
import eneter.net.system.EventHandler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class AndroidNetCommunicationClientActivityInner extends Activity {

    // UI controls
    private Handler myRefresh = new Handler();
    private EditText myMessageTextEditText;
    private EditText myResponseEditText;
    private Button mySendRequestBtn;

    // Sender sending MyRequest and as a response receiving MyResponse.
    private IDuplexTypedMessageSender<MyResponse, MyRequest> mySender;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tcp_server);

        // Get UI widgets.
        myMessageTextEditText = (EditText) findViewById(R.id.messageTextEditText);
        myResponseEditText = (EditText) findViewById(R.id.messageLengthEditText);
        mySendRequestBtn = (Button) findViewById(R.id.sendRequestBtn);

        // Subscribe to handle the button click.
        mySendRequestBtn.setOnClickListener(myOnSendRequestClickHandler);

        try {
            openConnection();
        } catch (Exception err) {
            EneterTrace.error("Open connection failed.", err);
        }
    }

    @Override
    public void onDestroy() {
        // Stop listening to response messages.
        mySender.detachDuplexOutputChannel();
    }

    private void openConnection() throws Exception {
        // Create sender sending MyRequest and as a response receiving
        // MyResponse
        IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
        mySender = aSenderFactory.createDuplexTypedMessageSender(
                MyResponse.class, MyRequest.class);

        // Subscribe to receive response messages.
        mySender.responseReceived().subscribe(myOnResponseHandler);

        // Create TCP messaging for the communication.
        // Note: 10.0.2.2 is a special alias to the loopback (127.0.0.1)
        // on the development machine
        IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
        IDuplexOutputChannel anOutputChannel = aMessaging
                .createDuplexOutputChannel("tcp://70.63.35.218/");

        // Attach the output channel to the sender and be able to send
        // messages and receive responses.
        mySender.attachDuplexOutputChannel(anOutputChannel);
    }

    private void onSendRequest(View v) {
        // Create the request message.
        MyRequest aRequestMsg = new MyRequest();
        aRequestMsg.Text = myMessageTextEditText.getText().toString();

        // Send the request message.
        try {
            mySender.sendRequestMessage(aRequestMsg);
        } catch (Exception err) {
            EneterTrace.error("Sending the message failed.", err);
        }
    }

    private void onResponseReceived(Object sender,
            final TypedResponseReceivedEventArgs<MyResponse> e) {
        // Display the result - returned number of characters.
        // Note: Marshal displaying to the correct UI thread.
        myRefresh.post(new Runnable() {
            public void run() {
                myResponseEditText.setText(Integer.toString(e
                        .getResponseMessage().Length));
            }
        });
    }

    private EventHandler<TypedResponseReceivedEventArgs<MyResponse>> myOnResponseHandler

    = new EventHandler<TypedResponseReceivedEventArgs<MyResponse>>() {
        public void onEvent(Object sender,
                TypedResponseReceivedEventArgs<MyResponse> e) {
            onResponseReceived(sender, e);
        }
    };

    private OnClickListener myOnSendRequestClickHandler = new OnClickListener() {
        public void onClick(View v) {
            onSendRequest(v);
        }
    };
}

我的请求Java

  package com.example.com.proto1;


public class MyRequest {
    // Request message type
    // The message must have the same name as declared in the service.
    // Also, if the message is the inner class, then it must be static.
    public String Text;
}

MyResponse Java

  package com.example.com.proto1;


public class MyResponse {
    // Request message type
    // The message must have the same name as declared in the service.
    // Also, if the message is the inner class, then it must be static.
    public int Length;
}

最佳答案

我不知道确切的答案,但我可以给你一个指导:

缺少的类是 com.example.com.proto1.AndroidNetCommunicationClientActivity$1,因为它以“$1”结尾,这意味着它是 AndroidNetCommunicationClientActivity 的内部类。

该内部类应该有一个名为 AndroidNetCommunicationClientActivity$1.class 的单独文件,因此似乎由于某种原因未生成该文件。

您可以尝试将内部类提取到实际的类中,看看是否有帮助,或者尝试找出未生成内部类文件的原因。

希望有帮助。

更新

评论太长了,所以我会尝试解释我的意思:

AndroidNetCommunicationClientActivityInner 内部,您必须声明内部类:MyRequest、MyResponse。

您需要创建 2 个新的 java 文件:MyRequest.java 和 MyResponse.java 并将代码复制到它们中,因此它看起来像:

MyRequest.java

 public class MyRequest {
        public String Text;
    }

MyResponse.java

public class MyResponse {
        public int Length;
    }

然后,从 AndroidNetCommunicationClientActivityInner删除这些内部类,并导入新创建的类,编译并重新部署代码。

关于java - 设置 tcp 服务器时出现 NoClassDefFound 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11829372/

相关文章:

android - Dagger 无法注入(inject)类型参数字段

java - 无法运行 JPad - NoClassDefFoundError javafx 场景父级

java - React Native : NoClassDefFoundError - android. support.v4.app.FragmentActivity

java - RandomAccessFileOrArray(byte[]) 的替代方法

java - Android 中的 transformClassesAndResourcesWithProguardForRelease' 错误

java - Do/While 循环中的输出出现问题

android - 为本地镜像创建适用于 Android 的照片库应用程序

新项目对话框中缺少 JavaFX FXML 应用程序。

android - Android 中的透明度问题

java - 测试中的 NoClassDefFoundError org/springframework/jdbc/core/ConnectionCallback