如何在MaincActivity内部调用类的方法

时间:2018-10-18 20:31:06

标签: java android-studio

我正在尝试构建使用蓝牙的android应用。我只有一个活动。我想从该活动中在 onCreate()方法中定义的类中调用方法 public void write()。 我收到此错误消息:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.example.rufusy.bluetoothconnect.MainActivity $ ConnectedThread.write(java.lang.String)'           在com.example.rufusy.bluetoothconnect.MainActivity $ 2.onClick(MainActivity.java:113)

我删除了一些代码,仅包含此代码。 我该如何解决这个问题?

ConnectedThread mConnectedThread; // bluetooth background worker thread to send and receive data

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


    mLED1 = findViewById(R.id.checkBoxLED1);

        /* turn on bluetooth radio */
        mLED1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bluetoothOn();
                mConnectedThread.write("toggle");
            }
        });
}

下面是实现我要调用的方法的代码。我正在调用public void write()方法;

 private class ConnectedThread extends Thread
{
    private final BluetoothSocket mmSocket;
    private final InputStream mmInputStream;
    private final OutputStream mmOutputStream;

    public ConnectedThread(BluetoothSocket socket)
    {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the input and output streams, using temp objects because
        // member streams are final

        try
        {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        }
        catch (IOException e) {}

        mmInputStream = tmpIn;
        mmOutputStream = tmpOut;
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(String input) {
        byte[] bytes = input.getBytes();           //converts entered String into bytes
        try {
            mmOutputStream.write(bytes);
        } catch (IOException e) { }
    }

}

我已经在另一个处理蓝牙套接字创建的方法中实例化了connectedThread类。

private AdapterView.OnItemClickListener mDeviceClickListener = new AdapterView.OnItemClickListener()
{
   /*
     more code here  
   */


  mConnectedThread = new ConnectedThread(mBTSocket);

   /*
     more code here  
   */

};

1 个答案:

答案 0 :(得分:0)

您应按照以下步骤创建线程:

private class ConnectedThread extends Thread
{
    private final BluetoothSocket mmSocket;
    private final InputStream mmInputStream;
    private final OutputStream mmOutputStream;
    boolean stopFlag;
    String dataToWrite;

    public ConnectedThread(BluetoothSocket socket)
    {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        stopFlag = false;
        dataToWrite = "";

        // Get the input and output streams, using temp objects because
        // member streams are final

        try
        {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        }
        catch (IOException e) {}

        mmInputStream = tmpIn;
        mmOutputStream = tmpOut;
    }

    @Override
    public void run() {
        while (stopFlag == false){
            if(dataToWrite != ""){
                byte[] bytes = dataToWrite.getBytes();           //converts entered String into bytes
                dataToWrite = "";
                try {
                    mmOutputStream.write(bytes);
                } catch (IOException e) { }
            }
            Thread.sleep(500);
        }
    }

    public void StopSending(){
        stopFlag = true;
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(String input) {
        dataToWrite = input;
    }
}

从该线程创建新实例后,可以多次调用write方法来写入数据。

请注意,当您不再需要发送(例如应用已关闭)时,请使用StopSending方法。要重新开始,请创建新实例。