2012-11-26 25 views
0

你好我運行谷歌的聊天應用程序,並且下面是代碼Android的「無級DEFF查找錯誤:org.jivesoftware.smack.ConnectionConfiguration」

import java.security.GeneralSecurityException; 
import java.util.ArrayList; 
import java.util.Collection; 

import javax.net.ssl.SSLContext; 

import org.jivesoftware.smack.ConnectionConfiguration; 
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode; 
import org.jivesoftware.smack.PacketListener; 
import org.jivesoftware.smack.Roster; 
import org.jivesoftware.smack.RosterEntry; 
import org.jivesoftware.smack.XMPPConnection; 
import org.jivesoftware.smack.XMPPException; 
import org.jivesoftware.smack.filter.MessageTypeFilter; 
import org.jivesoftware.smack.filter.PacketFilter; 
import org.jivesoftware.smack.packet.Message; 
import org.jivesoftware.smack.packet.Packet; 
import org.jivesoftware.smack.packet.Presence; 
import org.jivesoftware.smack.util.StringUtils; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 

public class FacebookChatActivity extends Activity implements OnClickListener { 

private ArrayList<String> messages = new ArrayList(); 
private Handler mHandler = new Handler(); 
private EditText mRecipient; 
private EditText mSendText; 
private ListView mList; 
private XMPPConnection mConnection; 
private String mHost, mPort, mService, mUsername, mPassword; 
private ConnectionConfiguration mConnConfig; 
private String TAG; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    initLayout(); 
    initGtalk(); 
// initFB(); 
// 
    // Create a connection 
    createConnection(); 

    // login 
    loginToXMPP(); 

} 

void initLayout() { 
    Log.i("XMPPClient", "onCreate called"); 
    setContentView(R.layout.activity_facebook_chat); 

    mRecipient = (EditText) this.findViewById(R.id.recipient); 
    Log.i("XMPPClient", "mRecipient = " + mRecipient); 
    mSendText = (EditText) this.findViewById(R.id.sendText); 
    Log.i("XMPPClient", "mSendText = " + mSendText); 
    mList = (ListView) this.findViewById(R.id.listMessages); 
    Log.i("XMPPClient", "mList = " + mList); 
    // Set a listener to send a chat text message 
    Button send = (Button) this.findViewById(R.id.send); 
    send.setOnClickListener(this); 

    setListAdapter(); 
} 

void initGtalk() { 
    mHost = "talk.google.com"; 
    mPort = "5222"; 
    mService = "gmail"; 
    mUsername = "[email protected]"; 
    mPassword = "password"; 
    // Set Default recipients for Gtalk 
    mRecipient.setText("[email protected]"); 
} 

void initFB() { 
    mHost = "chat.facebook.com"; 
    mPort = "5222"; 
    mService = "xmpp"; 
    mUsername = "[email protected]"; 
    mPassword = "password"; 
    // Set Default recipients for FB 
    mRecipient.setText("[email protected]"); 
} 

void createConnection() { 
    mConnConfig = new ConnectionConfiguration(mHost, 
      Integer.parseInt(mPort), mService); 
    mConnConfig.setSecurityMode(SecurityMode.required); 
    mConnConfig.setSASLAuthenticationEnabled(true); 
    mConnection = new XMPPConnection(mConnConfig); 

    try { 
     mConnection.connect(); 
     Log.i("XMPPClient", 
       "[SettingsDialog] Connected to " + mConnection.getHost()); 
    } catch (XMPPException ex) { 
     Log.e("XMPPClient", "[SettingsDialog] Failed to connect to " 
       + mConnection.getHost()); 
     Log.e("XMPPClient", ex.toString()); 
     setConnection(null); 
    } 
} 

void loginToXMPP() { 
    try { 
     mConnection.login(mUsername, mPassword); 
     Log.i("XMPPClient", "Logged in as " + mConnection.getUser()); 

     // Set the status to available 
     Presence presence = new Presence(Presence.Type.available); 
     mConnection.sendPacket(presence); 
     setConnection(mConnection); 
    } catch (XMPPException ex) { 
     Log.e("XMPPClient", "[SettingsDialog] Failed to log in as " 
       + mUsername); 
     Log.e("XMPPClient", ex.toString()); 
     setConnection(null); 
    } 
} 


/* * Called by Settings dialog when a connection is established with the XMPP 
* server 
* 
* @param connection*/ 

public void setConnection(XMPPConnection connection) { 
    this.mConnection = connection; 
    if (connection != null) { 
     // Add a packet listener to get messages sent to us 
     PacketFilter filter = new MessageTypeFilter(Message.Type.chat); 
     connection.addPacketListener(new PacketListener() { 
      public void processPacket(Packet packet) { 
       Message message = (Message) packet; 
       if (message.getBody() != null) { 
        String fromName = StringUtils.parseBareAddress(message 
          .getFrom()); 
        Log.i("XMPPClient", "Got text [" + message.getBody() 
          + "] from [" + fromName + "]"); 
        messages.add(fromName + ":"); 
        messages.add(message.getBody()); 
        // Add the incoming message to the list view 
        mHandler.post(new Runnable() { 
         public void run() { 
          setListAdapter(); 
         } 
        }); 
       } 
      } 
     }, filter); 
    } 
} 

private void setListAdapter() { 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.multi_line_list_item, messages); 
    mList.setAdapter(adapter); 
} 

public void onClick(View v) { 
    if (v.getId() == R.id.send) { 
     String to = mRecipient.getText().toString(); 
     String text = mSendText.getText().toString(); 

     Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]"); 
     Message msg = new Message(to, Message.Type.chat); 
     msg.setBody(text); 
     mConnection.sendPacket(msg); 
     messages.add(mConnection.getUser() + ":"); 
     messages.add(text); 
     setListAdapter(); 
    } 

} 

} 

當我運行這段代碼我得到

11-26 16:20:15.283: E/dalvikvm(595): Could not find class org.jivesoftware.smack.ConnectionConfiguration', referenced from method com.example.sarojfacebookchat.FacebookChatActivity.createConnection 

任何人都可以解釋我關於這個錯誤,如果需要請建議我正確的代碼。 我已經包括asmack2010.05.07.jar到我的項目

+0

你能看到項目propeties-> java build path-> libraries標籤下添加了jar嗎? – juned

+1

也檢查用戶對jar的權限。 – GamDroid

回答

1

只有三個原因,你永遠不會得到這個錯誤:

  1. 類真的不存在。如果您使用的是官方示例中的代碼並獲取該代碼,請確保您具有庫的最新版本
  2. 您尚未將jar添加到您的構建路徑。要解決這個問題,請右鍵單擊Eclipse中的jar,然後執行構建路徑►添加到構建路徑。
  3. 您的jar不在/libs文件夾中。當您將jar添加到構建路徑時會發生這種情況,但ADT的更新版本需要它在/libs中。把它放在那裏並重新添加到構建路徑。

大多數情況下會出現此類錯誤,因爲ADT的新版本要求所有外部罐子都在/libs文件夾中。您的同事可能與您的版本不同,因此錯誤。

1

你應該試試這個:

在您的項目從Java中刪除對JAR的所有引用構建路徑 - >你的代碼不應該建立一個沒有錯誤了

如果項目根目錄中不存在libs文件夾將JAR複製到libs文件夾中。

如果仍然沒有運行正常。右鍵單擊您的項目> Android工具>修復項目屬性

清理您的項目並運行。它會工作

0

試試這個方法...

  1. 右鍵單擊您的項目並轉到屬性。
  2. 去java構建路徑..//這是在左側的第5個位置
  3. 去訂購和導出選項卡。
  4. 檢查(刻度線)在您選擇的jar文件上。並點擊確定。
  5. 現在,清理你的項目並運行。
0

只是有這個問題太........ 解決的辦法是刪除構建路徑嫌罐子和複製的jar文件粘貼到項目的libs文件夾。