【问题标题】:Access a method from another class [closed]从另一个类访问方法[关闭]
【发布时间】:2013-06-14 14:51:14
【问题描述】:

按照你的推荐,我上了三等功!!

public class WebSocket_Connector {

private static final String TAG = "ECHOCLIENT";
public final WebSocketConnection mConnection = new WebSocketConnection();

public void connect(final String wsuri) {

      Log.d(TAG, "Connecting to: " + wsuri); 

      try {
         mConnection.connect(wsuri, new WebSocketHandler() {

            @Override
            public void onOpen() {
               Log.d(TAG, "Status: Connected to " + wsuri ); 
               Log.d(TAG, "Connection successful!\n");
            }

            @Override
            public void onTextMessage(String payload) {
               Log.d(TAG, "Got echo: " + payload);
            }

            @Override
            public void onClose(int code, String reason) {
               Log.d(TAG, "Connection closed.");
            }
         });
      } catch (WebSocketException e) {

         Log.d(TAG, e.toString());
      }
   }

}

在另一个类中,我正在尝试通过字符串类型“id”访问连接

public class Myoffers_Fragment extends Fragment {

private static final String TAG = "ECHOCLIENT";
public String id;

public static Fragment newInstance(Myoffers context, int pos, 
        float scale)
{
    Bundle b = new Bundle();
    b.putInt("pos", pos);
    b.putFloat("scale", scale);
    return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }
    WebSocket_Connector A = new WebSocket_Connector();

    LinearLayout l = (LinearLayout) 
            inflater.inflate(R.layout.mf, container, false);

    int pos = this.getArguments().getInt("pos");
    TextView tv = (TextView) l.findViewById(R.id.text);
    tv.setText("Position = " + pos);

    ImageView product_photo = (ImageView) l.findViewById(R.id.myoffer_image);

    switch(pos){
    case 0:
        product_photo.setImageResource(R.drawable.myoffers_0);
        Log.d(TAG, "Current pos" + pos);
        Toast.makeText(this.getActivity(), "Product" + pos + " is selected.", Toast.LENGTH_LONG).show();
        id = "product 0";
        A.mConnection.sendTextMessage(id);
        break;

你能看到“A.mConnection.sendTextMessage(id);”吗?? 这是正确的做法吗?

【问题讨论】:

  • 为什么这个问题没有显示任何努力???我寻找其他问题,但那些无法解决我的问题!
  • 首先,mConnection 没有在哪里声明。其次,您没有显示sendTextMessage(id) 的代码,就我们所知,这可能是一种不存在的方法。第三,id 声明在哪里?澄清你的问题,你可能会得到帮助。从对答案的评论中,这不应该也被标记为 Android 吗?
  • 声明了mConnection和sendTextMessage(id)和id。
  • 您遇到的错误是什么?编译时间,在你的 IDE 中?目前还不清楚需要什么样的帮助。据我所知,你永远不会在A 上调用connect,这可能是sendTextMessage 不起作用的原因。
  • @JonathanDrapeau 在连接器类中,它生成包含在库 jar 文件中的“WebSocketConnection”类。我想通过另一个类的“连接器”类访问“WebSocketConnection”。我想问我如何从另一个类访问“WebSocketConnection”?清楚了吗?

标签: java class methods


【解决方案1】:

您需要创建类的实例才能访问其公共方法。考虑到您的连接方法在您的主类中,您必须声明:Main main = new Main();(另请注意,您应该始终将您的类名大写)。然后拨打main.connect();

【讨论】:

  • 我不认为你可以对 Android Activity 类这样做。最好的办法可能是将连接移动到您可以实例化的第三个实用程序类,并在 mainMyoffers_Fragment 类中使用它。
  • 它不工作。我补充说, main Main = new main();和 Main.connect();但第二个出错了。
  • @vhallac 你是什么意思移动到第三个实用程序类?
  • @vhallac 我完全同意。
  • @JungHur public class Connector { public void connect() { ... } } 之类的。管理连接的第三类。也适用于关注点分离。
【解决方案2】:

代码应该是不言自明的

 class A {
    public void someMethod(){
       System.out.println("in someMethod");
    }
 }

 public class B {

    public static void main(String [] args){
        A a = new A(); // first create the object of class A
        a.someMethod();// call its method in class B
    }

 }

【讨论】:

  • 正如你提到的,我放了“main a = new main(); 和 a.connect();,但是第二个出错了。
猜你喜欢
  • 2013-01-05
  • 2022-12-05
  • 1970-01-01
  • 2012-12-05
  • 2017-05-12
  • 1970-01-01
  • 2014-12-17
  • 2012-03-19
  • 2015-01-21
相关资源
最近更新 更多