无法解析符号'connect'

时间:2017-06-28 11:38:00

标签: java android xmpp smack

这里有什么问题?为什么我总是无法解决符号'连接'。我从文档中获得了这些代码,而且我的依赖关系是正确的。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;

public class MainActivity extends AppCompatActivity {
    AbstractXMPPConnection conn1 = new XMPPTCPConnection("username", "password" ,"jabber.org");
conn1.connect();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

1 个答案:

答案 0 :(得分:0)

你不能这样做:

public class MainActivity extends AppCompatActivity {
    AbstractXMPPConnection conn1 = new XMPPTCPConnection("username", "password" ,"jabber.org");
conn1.connect();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

因为conn1.connect()未在有效范围内调用...(应在方法内部完成) 而是做

public class MainActivity extends AppCompatActivity {
    AbstractXMPPConnection conn1 = new XMPPTCPConnection("username", "password" ,"jabber.org");

     public void connectToSomething()   ... some exceptions here must be thrown{

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

    }
}