2015-05-19 21 views
2

我想为我的项目使用Smack。我以前在一些早期的项目中使用过aSmack,这些项目工作得很好。然而,Smack给了我怪异的例外。当在Android上使用Smack 4.1时,初始化异常“NoClassDefFoundError:javax.naming.directory.InitialDirContext”

这里是我的初始化代码看起来像 -

public void init(String username, String password) throws IOException, XMPPException, SmackException { 
     mConnection = new XMPPTCPConnection(username, password, HOST_NAME); //This is the line which generates the exception 

     mConnection.connect(); 
     mConnection.login(); 

     mChatManager = ChatManager.getInstanceFor(mConnection); 
     mChatManager.addChatListener(this); 
    } 

这里的logcat的输出 -

java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:3690) 
      at android.view.View.performClick(View.java:4192) 
      at android.view.View$PerformClick.run(View.java:17248) 
      at android.os.Handler.handleCallback(Handler.java:615) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4950) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at android.view.View$1.onClick(View.java:3685) 
            at android.view.View.performClick(View.java:4192) 
            at android.view.View$PerformClick.run(View.java:17248) 
            at android.os.Handler.handleCallback(Handler.java:615) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4950) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.ExceptionInInitializerError 
      at test.Managers.XMPPManager.init(XMPPManager.java:42) 
      at test.LoginActivity.onLoginClick(LoginActivity.java:46) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at android.view.View$1.onClick(View.java:3685) 
            at android.view.View.performClick(View.java:4192) 
            at android.view.View$PerformClick.run(View.java:17248) 
            at android.os.Handler.handleCallback(Handler.java:615) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4950) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.ExceptionInInitializerError 
      at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96) 
      at org.jivesoftware.smack.AbstractXMPPConnection.<clinit>(AbstractXMPPConnection.java:98) 
            at test.Managers.XMPPManager.init(XMPPManager.java:42) 
            at test.LoginActivity.onLoginClick(LoginActivity.java:46) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at android.view.View$1.onClick(View.java:3685) 
            at android.view.View.performClick(View.java:4192) 
            at android.view.View$PerformClick.run(View.java:17248) 
            at android.os.Handler.handleCallback(Handler.java:615) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4950) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.ExceptionInInitializerError 
      at java.lang.Class.classForName(Native Method) 
      at java.lang.Class.forName(Class.java:217) 
      at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213) 
      at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193) 
      at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163) 
      at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148) 
      at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116) 
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96) 
            at org.jivesoftware.smack.AbstractXMPPConnection.<clinit>(AbstractXMPPConnection.java:98) 
            at test.Managers.XMPPManager.init(XMPPManager.java:42) 
            at test.LoginActivity.onLoginClick(LoginActivity.java:46) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at android.view.View$1.onClick(View.java:3685) 
            at android.view.View.performClick(View.java:4192) 
            at android.view.View$PerformClick.run(View.java:17248) 
            at android.os.Handler.handleCallback(Handler.java:615) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4950) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NoClassDefFoundError: javax.naming.directory.InitialDirContext 
      at org.jivesoftware.smack.util.dns.javax.JavaxResolver.<clinit>(JavaxResolver.java:50) 
            at java.lang.Class.classForName(Native Method) 
            at java.lang.Class.forName(Class.java:217) 
            at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213) 
            at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193) 
            at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163) 
            at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148) 
            at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116) 
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96) 
            at org.jivesoftware.smack.AbstractXMPPConnection.<clinit>(AbstractXMPPConnection.java:98) 
            at test.Managers.XMPPManager.init(XMPPManager.java:42) 
            at test.LoginActivity.onLoginClick(LoginActivity.java:46) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at android.view.View$1.onClick(View.java:3685) 
            at android.view.View.performClick(View.java:4192) 
            at android.view.View$PerformClick.run(View.java:17248) 
            at android.os.Handler.handleCallback(Handler.java:615) 
            at android.os.Handler.dispatchMessage(Handler.java:92) 
            at android.os.Looper.loop(Looper.java:137) 
            at android.app.ActivityThread.main(ActivityThread.java:4950) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
            at dalvik.system.NativeStart.main(Native Method) 

我已经检查了一些论坛帖子它说,启用ProGuard的,当这个问题出现。但是在我的项目Progaurd被禁用。

任何想法可能会导致此异常?

更新: -
这是我app.gradle文件看起来像 -

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'org.igniterealtime.smack:smack-java7:4.1.0' 
    // Optional for XMPPTCPConnection 
    compile 'org.igniterealtime.smack:smack-tcp:4.1.0' 
    // Optional for XMPP-IM (RFC 6121) support (Roster, Threaded Chats, …) 
    compile 'org.igniterealtime.smack:smack-im:4.1.0' 
    // Optional for XMPP extensions support 
    compile 'org.igniterealtime.smack:smack-extensions:4.1.0' 
    compile 'com.android.support:appcompat-v7:21.0.3' 
    compile project(':xxx') 
    compile project(':xxx') 
    compile project(':xxx') 
    compile('com.crashlytics.sdk.android:crashlytics:[email protected]') { 
     transitive = true; 
    } 
} 
+0

你读过Smack的自述吗?你是如何将Smack添加到你的项目中的? – Flow

+0

@流动感谢您的回复。我使用Gradle来添加依赖关系。请检查问题中的更新部分,看看我添加了什么。 – noob

+0

然后我阅读文档部分。该代码取自“入门”部分。 – noob

回答

7

不要在Android项目添加咂嘴,java7,使用咂嘴,机器人代替。

另外Smack 4.2.0是最新的Smack版本。

+0

现在我觉得自己忽略了这个细节是愚蠢的。谢谢您的帮助。它确实有效。 – noob

相关问题