2012-08-07 19 views
0

我正在安裝一個tcp服務器,我能夠得到示例工作,但沒有將示例應用到我自己的應用程序。我不斷收到一個NoClassDefFound錯誤,並嘗試刪除內部類並創建單獨的類,但它似乎沒有工作。我已經包含了我的LogCats和java文件。

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) 

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

曾經是內部類

public class MyRequest { 
    public String Text; 
} 

曾經是內第二類

public class MyResponse { 
    public int Length; 
} 

清單

<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> 
+0

你在使用任何外部'.jar'文件嗎?如果是,則請嘗試[this](http://stackoverflow.com/a/11824038/940096)或者,您是否在AndroidManifest.xml文件中聲明瞭所有活動? – Praveenkumar 2012-08-07 14:46:57

+0

我現在正在添加清單。我正在使用一個外部jar,但我已正確放入。 – 2012-08-07 15:21:34

+0

@SamBevins你能指出代碼中的第103行嗎? $符號表示匿名內部類。 – 2012-08-07 16:34:39

回答

2

我不能確定有關錯誤的,但$ 1指一樣,你在這裏使用

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

從技術上講,你是一個匿名內部類(沒有名字的類)創建一個名爲$ 1的Runnable的子類,並且這個類是內部的。不知道錯誤是什麼,雖然我會嘗試重新編譯所有東西

+0

奇怪的是,編碼與我的工作示例相同,不會返回任何錯誤。我如何重新編譯? – 2012-08-07 15:20:19

+0

@SamBevins然後嘗試清理您的項目,然後再試一次,或重新啓動IDE – 2012-08-07 15:37:38

+0

我已經做到了多次,但我仍然有同樣的錯誤在我的問題 – 2012-08-07 15:57:17

0

你還沒有重新編譯所有東西。無論是你還是你的類路徑中有一個任性的入口。

0

我想我在想通了這個問題,

在清單文件的包被宣佈爲AndroidNetCommunicationClientActivityInner作爲

package="com.example.com.proto1" 

但實際上你沒有在包類,而不是因爲在指定的JAVA文件中沒有包聲明。

但是android會尋找該類的包,因此會拋出java.lang.NoClassDefFoundError

+0

好吧,我更換了兩個班,但我仍然有完全相同的問題 – 2012-08-08 13:25:08

+0

@SamBevins問題是最有可能與包聲明的東西,你試圖找出它? – 2012-08-08 13:47:27

+0

我對編程相當陌生,所以我無法弄清楚問題所在。我試圖弄明白。我不明白的是我的示例代碼如何工作,但在我的實際應用程序中使用時不起作用。我檢查了所有的編碼,但它看起來是一樣的 – 2012-08-08 14:17:12