当在Android上使用Smack

当在Android上使用Smack

本文介绍了初始化异常“NoClassDefFoundError:javax.naming.directory.InitialDirContext”当在Android上使用Smack 4.1时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Smack作为我的项目。我以前在我早期的一些项目中使用了一个工作非常好的项目。但是,Smack是给我奇怪的例外。



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

  public void init(String username,String password)throws IOException,XMPPException,SmackException {
mConnection = new XMPPTCPConnection(username,password,HOST_NAME); //这是生成异常的行

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

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

这是Logcat输出 -

  java.lang.IllegalStateException:无法在android.view.View执行活动
$ 1.onClick(View.java:3690)
在android.view.View.performClick(View.java:4192)
在android.view.View $ PerformClick.run(View.java:17248)
在android.os.Handler.handleCallback(处理程序.java:615)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android。 app.ActivityThread.main(ActivityThread.java:4950)
在java.lang.reflect.Method.invokeNative(本机方法)
在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)
在dalvik.system.NativeStart.main(本机方法)
导致:java.lang.reflect.InvocationTargetException
在java.lang.reflect.Method.invokeNative(本机方法)
在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)
在android.view.View $ PerformClick.run(View.java:17248)
在android.os.Handler.handleCallback(Handler.java:615)
在android .os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4950 )
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:511)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:997)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
在dalvik .system.NativeStart.main(Native Method)
引起的:java.lang.ExceptionInInitializerError
at test.Managers.XMPPManager.init(XMPPManager.java:42)
at test.LoginActivity。 onLoginClick(LoginActivity.java:46)
java.lang.reflect.Method.invokeNative(Native Method)
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)
在android.os.Handler.handleCallback(Handler.java:615)
在android.os .Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4950)
java.lang.reflect.Method.invokeNative(Native Method)
在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方法)
导致:java.lang.ExceptionInInitializerError
在org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
在org.jivesoftware.smack.AbstractXMPPConnection。< clinit>(AbstractXMPPConnection.java:98)
在test.Managers.XMPPManager.init(XMPPManager .java:42)
at test.LoginActivity.onLoginClick(LoginActivity.java:46)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect .Method.invoke(Method.java:511)
在android.view.View $ 1.onClick(View.java:3685)
在android.view.View.performClick(View.java:4192)
在android.view.View $ PerformClick.run(View.java:17248)
在android.os.Handler.handleCallback(Handler.java:615)
在android.os.Handler .dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4950)
java.lang.reflect.Method.invokeNative(Native Method)
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)
在dalvik.system.NativeStart.main(Native Method)
引起的:java.lang.ExceptionInInitializerError
java.lang.Class.classForName(Native Method)
在java.lang.Class .forName(Class.java:217)
在org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213)
在org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193 )
在org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163)
在org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
在org。 jivesoftware.smack.SmackInitialization< clinit>(SmackInitializat ion.java:116)
在org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
在org.jivesoftware.smack.AbstractXMPPConnection。< clinit>(AbstractXMPPConnection.java:98 )
at test.Managers.XMPPManager.init(XMPPManager.java:42)
at test.LoginActivity.onLoginClick(LoginActivity.java:46)
在java.lang.reflect.Method。 invokeNative(Native Method)
在java.lang.reflect.Method.invoke(Method.java:511)
在android.view.View $ 1.onClick(View.java:3685)
在android.view.View.performClick(View.java:4192)
在android.view.View $ PerformClick.run(View.java:17248)
在android.os.Handler.handleCallback(处理程序.java:615)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:4950)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在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)
导致:java.lang.NoClassDefFoundError: javax.naming.directory.InitialDirContext
在org.jivesoftware.smack.util.dns.javax.JavaxResolver。< clinit>(JavaxResolver.java:50)
在java.lang.Class.classForName( Native方法)
在java.lang.Class.forName(Class.java:217)
在org.jives oftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213)
在org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193)
在org.jivesoftware.smack.SmackInitialization.processConfigFile( SmackInitialization.java:163)
在org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
在org.jivesoftware.smack.SmackInitialization。< clinit>(SmackInitialization.java:116 )
在org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
在org.jivesoftware.smack.AbstractXMPPConnection。< clinit>(AbstractXMPPConnection.java:98)
at test.Managers.XMPPManager.init(XMPPManager.java:42)
在test.LoginActivity.onLoginClick(LoginActivity.java:46)
在java.lang.reflect.Method.invokeNative(Native方法)
在java.lang.reflect.Method.invoke(Method.java:511)
在android.view.View $ 1.onClick(查看.java:3685)
在android.view.View.performClick(View.java:4192)
在android.view.View $ PerformClick.run(View.java:17248)
在android.os.Handler.handleCallback(Handler.java:615)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java: 137)
在android.app.ActivityThread.main(ActivityThread.java:4950)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect。 Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:997)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
在dalvik.system.NativeStart.main(本机方法)

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



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



更新: -

这是我的app.gradle文件的样子 -

 依赖项{ 
编译fileTree(dir:'libs',include:['* .jar'])
compile'org.igniterealtime.smack:smack-java7:4.1.0'
//可选for XMPPTCPConnection
compile'org.igniterealtime.smack:smack-tcp:4.1.0'
// XMPP-IM(RFC 6121)支持的可选(名册,线程聊天,...)
编译'org.igniterealtime.smack:smack-im:4.1.0'
//可选XMPP扩展支持
compile'org.igniterealtime.smack:smack-extensions:4.1.0'
编译'com.android.support:appcompat-v7:21.0.3'
编译项目(':xxx')
编译项目(':xxx')
编译项目(': xxx')
compile('com.crashlytics.sdk.android:crashlytics:2.2.4@aar'){
transitive = true;
}
}


解决方案

在Android项目中添加smack-java7,使用smack-android。



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


I am trying to use Smack for my project. I have previously used aSmack in some of my earlier projects which worked really well. However Smack is giving me weird exceptions.

Here's my initialization code looks like -

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

Here's the Logcat output -

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)

I have checked some forum posts which says that this problem comes when proguard is enabled. However in my project Progaurd is disabled.

Any idea what could cause this exception?

Update:-
This is how my app.gradle file looks like -

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:2.2.4@aar') {
        transitive = true;
    }
}
解决方案

Don't add smack-java7 on Android projects, use smack-android instead.

Also Smack 4.2.0 is the latest Smack version.

这篇关于初始化异常“NoClassDefFoundError:javax.naming.directory.InitialDirContext”当在Android上使用Smack 4.1时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:06