Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓:蓝牙聊天。从另一个类连接到MainActivity中的mHandler_Android_Bluetooth_Handler - Fatal编程技术网

Android 安卓:蓝牙聊天。从另一个类连接到MainActivity中的mHandler

Android 安卓:蓝牙聊天。从另一个类连接到MainActivity中的mHandler,android,bluetooth,handler,Android,Bluetooth,Handler,我制作了android蓝牙聊天教程。我希望在另一个类(MyCursorAdapter.class)中使用OnClickListeners,而不是在主活动中使用OnClickListeners 我认为这是一个非常简单的java问题。但我不知道如何实现这一点(我如何使用我班上其他活动中的mHandler) 有人能帮我吗 为菲利克斯干杯 MainActivity.class中的源(运行良好) MyCursorAdapter.class中的源代码(这里我想实现相同的函数) 评论: 主活动中MyCurso

我制作了android蓝牙聊天教程。我希望在另一个类(MyCursorAdapter.class)中使用OnClickListeners,而不是在主活动中使用OnClickListeners

我认为这是一个非常简单的java问题。但我不知道如何实现这一点(我如何使用我班上其他活动中的mHandler)

有人能帮我吗

为菲利克斯干杯

MainActivity.class中的源(运行良好)

MyCursorAdapter.class中的源代码(这里我想实现相同的函数)

评论:

主活动中MyCursorAdapter的实例

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);


    adapter = new MyCursorAdapter(this, notesCursor);
    setListAdapter(adapter);
}
评论:

或者如何从MyCursorAdapter调用此MainActivity方法?:

public void talkChat(byte[] out) { 
   mChatService.write(out); 
   mChatService = new BluetoothService(this, mHandler); 
 }

在MyCursorAdapter类中创建mhandler的实例

     public class MyCursorAdapter extends ResourceCursorAdapter implements  
    OnClickListener {

    private Handler mHandler;

    public void setMHandler(Handler mHandler){
        this.mHandler = mHandler;

    private class OffItemClickListener implements OnClickListener {
    private int position;

    public OffItemClickListener(int position) {
        super();
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        mCursor.moveToPosition(position);
           // byte[] out = {32};
           // MainAcitivity.talkChat(out);  
           // error: "non-static variable this cannot be referenced from a static context"

             Use your mhandler...
           // Here i want to the the same like in the MainActivity class. 
           // But how can I connect to the mHandler?         
    }
    }
    }
并从您的主要活动中引用它

private void fillData() {
  Cursor notesCursor = mDbHelper.fetchAllNotes();
 startManagingCursor(notesCursor);

adapter = new MyCursorAdapter(this, notesCursor);
adapter.setMHandler(mHandler); //Reference  
setListAdapter(adapter);

}在MyCursorAdapter类中创建mhandler的实例

     public class MyCursorAdapter extends ResourceCursorAdapter implements  
    OnClickListener {

    private Handler mHandler;

    public void setMHandler(Handler mHandler){
        this.mHandler = mHandler;

    private class OffItemClickListener implements OnClickListener {
    private int position;

    public OffItemClickListener(int position) {
        super();
        this.position = position;
    }

    @Override
    public void onClick(View v) {
        mCursor.moveToPosition(position);
           // byte[] out = {32};
           // MainAcitivity.talkChat(out);  
           // error: "non-static variable this cannot be referenced from a static context"

             Use your mhandler...
           // Here i want to the the same like in the MainActivity class. 
           // But how can I connect to the mHandler?         
    }
    }
    }
并从您的主要活动中引用它

private void fillData() {
  Cursor notesCursor = mDbHelper.fetchAllNotes();
 startManagingCursor(notesCursor);

adapter = new MyCursorAdapter(this, notesCursor);
adapter.setMHandler(mHandler); //Reference  
setListAdapter(adapter);

}

没有MyCursorAdapter执行Ext和ResourceCursorAdapter。我把它添加到我的帖子里。起初,我试图为主actity
public void talkChat(byte[]out){mChatService.write(out);mChatService=new BluetoothService(this,mHandler);}
创建一个新方法。但是当我从MyCursorAdapter调用它时,我收到消息“不能对非静态方法进行静态引用”。在MainActivity类中是否有MyCursorAdapter的实例?是的,在Main Activity类中有MyCursorAdapter的实例(请参阅我文章中的注释)。第二条评论是我尝试的第一件事。但是我不能用MyAcitivity.talkChat(…)从MyCursorAdapter.java调用它。没有MyCursorAdapter做ext和ResourceCursorAdapter。我把它添加到我的帖子里。起初,我试图为主actity
public void talkChat(byte[]out){mChatService.write(out);mChatService=new BluetoothService(this,mHandler);}
创建一个新方法。但是当我从MyCursorAdapter调用它时,我收到消息“不能对非静态方法进行静态引用”。在MainActivity类中是否有MyCursorAdapter的实例?是的,在Main Activity类中有MyCursorAdapter的实例(请参阅我文章中的注释)。第二条评论是我尝试的第一件事。但是我不能用MyActivity.talkChat(…)从MyCursorAdapter.java调用它