Xmpp 应用程序崩溃说';不幸的是,应用程序已停止';使用asmack时

Xmpp 应用程序崩溃说';不幸的是,应用程序已停止';使用asmack时,xmpp,asmack,Xmpp,Asmack,下面是我通过aSmack API连接到OpenFire的代码。尝试在手机上运行应用程序时,我收到一个错误,提示“不幸的是,应用程序已停止”。当我删除aSmack的代码时,该应用程序可以很好地创建一个按钮。我还将jar添加到类路径中。请帮帮我 package com.example.demo; import java.io.File; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.sm

下面是我通过aSmack API连接到OpenFire的代码。尝试在手机上运行应用程序时,我收到一个错误,提示“不幸的是,应用程序已停止”。当我删除aSmack的代码时,该应用程序可以很好地创建一个按钮。我还将jar添加到类路径中。请帮帮我

package com.example.demo;

import java.io.File;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

String username = "swapnil", password = "swapnil", host = "192.168.0.4", service = "mirana";
int port = 5222;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button bt = (Button) findViewById(R.id.button1);
    final EditText et =(EditText)findViewById(R.id.editText1);

    bt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        //  et.setText("Test");
            XMPPConnection xmpp = null;
            ConnectionConfiguration xmppConfig = new ConnectionConfiguration("192.168.0.4", 5222,"mirana");
            xmppConfig.setSASLAuthenticationEnabled(true);
            xmppConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
            if (xmppConfig == null)
                throw new NullPointerException("XMPPService must be configured before it can connect!");
            try {
                if (xmpp == null) {
                    xmpp = new XMPPConnection(xmppConfig);
                }
                xmpp.connect();
            } catch (XMPPException ex) {
                et.setText("ERROR !");
            }


        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}
这是清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.demo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

在没有日志的情况下,在查看您的代码之后,我可以猜到,您正在主线程上运行smack方法。。异常应为
NetworkOnMainThreadException

您应该在后台线程上单击按钮来完成任务。您可能需要为此创建
AsyncTask

另外,你没有互联网许可。所以,请也解决这个问题


在实现Smack时,您需要注意维护Smack连接和相关连接的会话。。为此,您可以参考GitHub演示应用程序中的以下内容:

您必须在AndroidManifest.xml中添加
Internet
权限


你能添加logcat日志吗?
<uses-permission android:name="android.permission.INTERNET" />
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);