0

I am setting up a tcp server in android and I am able to get the sample working but not implementing the sample on to my own app. I keep getting a NoClassDefFound error and have tried removing inner classes and making separate classes but it doesn't appear to be working. I have included my LogCats and java files.

LogCat

08-07 14:34:03.642: W/dalvikvm(332): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-07 14:34:03.642: E/AndroidRuntime(332): FATAL EXCEPTION: main
08-07 14:34:03.642: E/AndroidRuntime(332): java.lang.NoClassDefFoundError: com.example.com.proto1.AndroidNetCommunicationClientActivityInner$1
08-07 14:34:03.642: E/AndroidRuntime(332):  at com.example.com.proto1.AndroidNetCommunicationClientActivityInner.<init>(AndroidNetCommunicationClientActivityInner.java:103)
08-07 14:34:03.642: E/AndroidRuntime(332):  at java.lang.Class.newInstanceImpl(Native Method)
08-07 14:34:03.642: E/AndroidRuntime(332):  at java.lang.Class.newInstance(Class.java:1409)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.os.Looper.loop(Looper.java:123)
08-07 14:34:03.642: E/AndroidRuntime(332):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-07 14:34:03.642: E/AndroidRuntime(332):  at java.lang.reflect.Method.invokeNative(Native Method)
08-07 14:34:03.642: E/AndroidRuntime(332):  at java.lang.reflect.Method.invoke(Method.java:507)
08-07 14:34:03.642: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-07 14:34:03.642: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-07 14:34:03.642: E/AndroidRuntime(332):  at dalvik.system.NativeStart.main(Native Method)

Main tcp java

import net.client.MyRequest;
import net.client.MyResponse;
import net.client.R;
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.main);

        // 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://10.0.2.2:8060/");

        // 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);
        }
    };
}

Class that used to be inner

public class MyRequest {
    public String Text;
}

2nd class that used to be inner

public class MyResponse {
    public int Length;
}

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.com.proto1"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <application
        android:icon="@drawable/theeye"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".mainj"
            android:label="@string/title_activity_mainj" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Infoactive"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.INFOSCREEN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VoicePrompts"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VOICEPROMPTS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VPon"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VPON" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".VPoff"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VPOFF" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <!-- android name must match the name of the java you want to use -->
        <activity
            android:name=".VoiceRecognition"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.RECOGNITIONMENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Recognition"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="ACTION_RECOGNIZE_SPEECH" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SpeakingAndroid"
            android:label="tts" >
            <intent-filter>
                <action android:name="android.intent.action.SPEAK" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AndroidNetCommunicationClientActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="ANDROID_NET" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".AndroidNetCommunicationClientActivityInner"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="ANDROID_NET2" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Sam Bevins
  • 1,939
  • 4
  • 21
  • 26
  • Are you using any external `.jar` files? If yes means, then try [this](http://stackoverflow.com/a/11824038/940096) Or, Did you declare all the activities in your `AndroidManifest.xml` file? – Praveenkumar Aug 07 '12 at 14:46
  • I am adding the manifest now. I am using an external jar but I have it correctly put in. – Sam Bevins Aug 07 '12 at 15:21
  • @SamBevins Can you point out the line 103 in the code? $ symbol indicates the anonymous inner class. – code-jaff Aug 07 '12 at 16:34
  • 103 is the first line of the private eventhandler – Sam Bevins Aug 07 '12 at 16:40
  • @SamBevins i'm pretty sure you followed this http://eneter.blogspot.de/2012/03/android-how-to-communicate-with-net.html, but the tutorial have used static inner class as message type. – code-jaff Aug 07 '12 at 18:09
  • the static inner classes didn't work properly. On my sample I had to remove them and make separate non-static classes – Sam Bevins Aug 07 '12 at 18:12
  • @SamBevins try my answer, it is better to put those classes as they were previously (static nested classes). – code-jaff Aug 07 '12 at 18:38

3 Answers3

2

I'm unsure about the error, but $1 refers to an anonymous inner class (a class with no name) like the one you use here

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

Technically you are creating a subclass of Runnable that is called $1 and is inner to this class. Not sure what the error is though I would try recompiling everything

James
  • 2,483
  • 2
  • 24
  • 31
0

You haven't recompiled everything. Either that or you've got a wayward entry in your classpath.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

I think i figured out the problem,

In the manifest file the package is declared for the AndroidNetCommunicationClientActivityInner as

package="com.example.com.proto1"

But actually you are not having that class in that package, instead in the default package since there is no package declaration in the specified JAVA file.

But android looks for that package for that class, therefore throws java.lang.NoClassDefFoundError.

code-jaff
  • 9,230
  • 4
  • 35
  • 56
  • Ok, I replaced the two classes but I am still having the exact same problem – Sam Bevins Aug 08 '12 at 13:25
  • @SamBevins The problem is most probably with the package declaration stuffs, have you tried to figure it out? – code-jaff Aug 08 '12 at 13:47
  • I'm fairly new to programming so I can't figure out what the problem is. I have tried to figure it out. The thing I don't understand is how my sample code works but doesn't work when used in my actual app. I have checked all the coding but it appears the same – Sam Bevins Aug 08 '12 at 14:17
  • @SamBevins ok, do you have this `package com.example.com.proto1;` at the top of your `AndroidNetCommunicationClientActivityInner` java file? – code-jaff Aug 08 '12 at 14:21