2013-12-17 3 views
-2

xmpp 채팅을 만드는 방법에 대한 지침을 따르려고합니다. 그러나 "R이 변수로 해석 될 수 없음"을 계속 보여주었습니다. import.android.R을 삽입 한 후 오류가 모든 변수로 이동했습니다. 모든 변수를 변수로 해석 할 수 없다는 사실을 알고 있습니다. 여기 "variable"을 변수로 해석 할 수 없습니다.

package com.demo.xmppchat; 

import java.util.ArrayList; 
import java.util.Collection; 

import org.jivesoftware.smack.ConnectionConfiguration; 
import org.jivesoftware.smack.PacketListener; 
import org.jivesoftware.smack.Roster; 
import org.jivesoftware.smack.RosterEntry; 
import org.jivesoftware.smack.SASLAuthentication; 
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.R; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListView; 

public class XMPPChatDemoActivity extends Activity { 

    public static final String HOST = "talk.google.com"; 
    public static final int PORT = 5222; 
    public static final String SERVICE = "gmail.com"; 
    public static final String USERNAME = "[email protected]"; 
    public static final String PASSWORD = "xxx"; 

    private XMPPConnection connection; 
    private ArrayList<String> messages = new ArrayList<String>(); 
    private Handler mHandler = new Handler(); 

    private EditText recipient; 
    private EditText textMessage; 
    private ListView listview; 

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

     recipient = (EditText) this.findViewById(R.id.toET); 
     textMessage = (EditText) this.findViewById(R.id.chatET); 
     listview = (ListView) this.findViewById(R.id.listMessages); 
     setListAdapter(); 

     // Set a listener to send a chat text message 
     Button send = (Button) this.findViewById(R.id.sendBtn); 
     send.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       String to = recipient.getText().toString(); 
       String text = textMessage.getText().toString(); 

       Log.i("XMPPChatDemoActivity", "Sending text " + text + " to " + to); 
       Message msg = new Message(to, Message.Type.chat); 
       msg.setBody(text);    
       if (connection != null) { 
        connection.sendPacket(msg); 
        messages.add(connection.getUser() + ":"); 
        messages.add(text); 
        setListAdapter(); 
       } 
      } 
     }); 

     connect(); 
    } 

    /** 
    * Called by Settings dialog when a connection is establised with the XMPP 
    * server 
    * 
    * @param connection 
    */ 
    public void setConnection(XMPPConnection connection) { 
     this.connection = 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() { 
       @Override 
       public void processPacket(Packet packet) { 
        Message message = (Message) packet; 
        if (message.getBody() != null) { 
         String fromName = StringUtils.parseBareAddress(message 
           .getFrom()); 
         Log.i("XMPPChatDemoActivity", "Text Recieved " + 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.listitem, messages); 
     listview.setAdapter(adapter); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     try { 
      if (connection != null) 
       connection.disconnect(); 
     } catch (Exception e) { 

     } 
    } 

    public void connect() { 

     final ProgressDialog dialog = ProgressDialog.show(this, 
       "Connecting...", "Please wait...", false); 

     Thread t = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       // Create a connection 
       ConnectionConfiguration connConfig = new ConnectionConfiguration(
         HOST, PORT, SERVICE); 
       XMPPConnection connection = new XMPPConnection(connConfig); 

       try { 
        connection.connect(); 
        Log.i("XMPPChatDemoActivity", 
          "Connected to " + connection.getHost()); 
       } catch (XMPPException ex) { 
        Log.e("XMPPChatDemoActivity", "Failed to connect to " 
          + connection.getHost()); 
        Log.e("XMPPChatDemoActivity", ex.toString()); 
        setConnection(null); 
       } 
       try { 
        // SASLAuthentication.supportSASLMechanism("PLAIN", 0); 
        connection.login(USERNAME, PASSWORD); 
        Log.i("XMPPChatDemoActivity", 
          "Logged in as " + connection.getUser()); 

        // Set the status to available 
        Presence presence = new Presence(Presence.Type.available); 
        connection.sendPacket(presence); 
        setConnection(connection); 

        Roster roster = connection.getRoster(); 
        Collection<RosterEntry> entries = roster.getEntries(); 
        for (RosterEntry entry : entries) { 
         Log.d("XMPPChatDemoActivity", 
           "--------------------------------------"); 
         Log.d("XMPPChatDemoActivity", "RosterEntry " + entry); 
         Log.d("XMPPChatDemoActivity", 
           "User: " + entry.getUser()); 
         Log.d("XMPPChatDemoActivity", 
           "Name: " + entry.getName()); 
         Log.d("XMPPChatDemoActivity", 
           "Status: " + entry.getStatus()); 
         Log.d("XMPPChatDemoActivity", 
           "Type: " + entry.getType()); 
         Presence entryPresence = roster.getPresence(entry 
           .getUser()); 

         Log.d("XMPPChatDemoActivity", "Presence Status: " 
           + entryPresence.getStatus()); 
         Log.d("XMPPChatDemoActivity", "Presence Type: " 
           + entryPresence.getType()); 
         Presence.Type type = entryPresence.getType(); 
         if (type == Presence.Type.available) 
          Log.d("XMPPChatDemoActivity", "Presence AVIALABLE"); 
         Log.d("XMPPChatDemoActivity", "Presence : " 
           + entryPresence); 

        } 
       } catch (XMPPException ex) { 
        Log.e("XMPPChatDemoActivity", "Failed to log in as " 
          + USERNAME); 
        Log.e("XMPPChatDemoActivity", ex.toString()); 
        setConnection(null); 
       } 

       dialog.dismiss(); 
      } 
     }); 
     t.start(); 
     dialog.show(); 
    } 
} 

그리고

오류 목록은 다음과 같습니다 : 여기

코드입니다

Description Resource Path Location Type 
toET cannot be resolved or is not a field XMPPChatDemoActivity.java /XMPPChatDemo/src/com/demo/xmppchat line 54 Java Problem 
main cannot be resolved or is not a field XMPPChatDemoActivity.java /XMPPChatDemo/src/com/demo/xmppchat line 52 Java Problem 
chatET cannot be resolved or is not a field XMPPChatDemoActivity.java /XMPPChatDemo/src/com/demo/xmppchat line 55 Java Problem 
listMessages cannot be resolved or is not a field XMPPChatDemoActivity.java /XMPPChatDemo/src/com/demo/xmppchat line 56 Java Problem 
sendBtn cannot be resolved or is not a field XMPPChatDemoActivity.java /XMPPChatDemo/src/com/demo/xmppchat line 60 Java Problem 
listitem cannot be resolved or is not a field XMPPChatDemoActivity.java /XMPPChatDemo/src/com/demo/xmppchat line 117 Java Problem 

내가 프로젝트 시도 -> 청소를하지만 여전히 작동하지 않습니다.

+0

com.demo.xmppchat.R를 선택했다. 'import android.R;'을 제거하고'import com.demo.xmppchat.R; '을 추가하십시오. – Fllo

+0

import android.R을 제거하십시오; –

+0

[R은 해결할 수 없습니다 - 안드로이드 오류] (0120-13756-0) – Geobits

답변

1

가져 오기

import android.R; 

아래의 수입에 대한 필요가 없습니다를 제거하지 않는 한 그 당신이 수입 packagename.R를 사용하는 경우 다른 패키지;

import com.demo.xmppchat.R; 
+0

@ user2683347'import com.demo.xmppchat. R;' – Raghunandan

+0

그 작동하지 않습니다 .. 수입 라인이 해결되지 않았다는 오류입니다. – elvirachrisanty

+1

** "import import com.demo.xmppchat.R;"** 다른 패키지에 있지 않으면 R.java 클래스를 가져올 필요가 없습니다. 암묵적으로 다르게 수행 될 것입니다. – Squonk

0

Ctrl 키에게

import android.R; 

그냥 누르면이 가져 오기를 제거 + 그것은 가져 오기를 요청합니다 시프트 + O

전 어떤 충돌을 만들면

android.R;com.demo.xmppchat.R;.

그럼 난 당신이 잘못된 파일을 가져올 생각

관련 문제