繁体   English   中英

单击按钮时,android onClick无法运行

[英]android onClick doesn't run when button is clicked on

所以我有这个类有5个按钮的处理程序。 他们应该运行两次线程,并从另一个类调用一个函数。 但是,当我单击它们时,它们什么也不做。 当用户单击按钮时,我想让这些按钮正常工作的地方是什么?

我定义处理程序的类:

package com.example.michael.bluetoothcontroll;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; 
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Set;
import java.util.UUID;
import java.lang.Runnable;

public class control extends ActionBarActivity {

BluetoothDevice mmDevice;
blueToothControl bt = new blueToothControl();

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_control, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

// Define the Handler that receives messages from the thread and update the progress
@Override
public void onCreate(final Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_control);

    final Button fwd   = (Button) findViewById(R.id.FWD);
    final Button rev   = (Button) findViewById(R.id.REV);
    final Button left  = (Button) findViewById(R.id.Left);
    final Button right = (Button) findViewById(R.id.Right);
    final Button start = (Button) findViewById(R.id.settings);


    /**
     *
     * render thread
     * used to update the UI
     *
     */
    final class render implements Runnable {

        // what to send to the TextView
        public String message;

        // constructor
        public render(String msg){
            message = msg;
        }

        public void run(){

            setContentView(R.layout.activity_control);
            TextView tv = (TextView) findViewById(R.id.confirm);
            tv.setText(message);
        }
    }

    /**
     *
     * button handlers
     *
     * */
    //start start button handler
    start.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            bt.callConnectThread(mmDevice);
        }
    });
    //end start button handler

    // start fwd button handler
    fwd.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String test = bt.callWorkThread("fwd");
            (new Thread(new render("sending fwd \n"))).start();
            (new Thread(new render(test))).start();
        }
    });
    //end fwd button handler

    //start left on button handler
    left.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            (new Thread(new render("sending left \n"))).start();
            (new Thread(new render(bt.callWorkThread("left")))).start();
        }
    });
    //end left button handler

    //start right  button handler
    right.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            (new Thread(new render("sending right \n"))).start();
            (new Thread(new render(bt.callWorkThread("right")))).start();

        }
    });
    // end right button handler

    //start reverse  button handler
    rev.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            (new Thread(new render("sending rev \n"))).start();
            (new Thread(new render(bt.callWorkThread("rev")))).start();

        }
    });
    // end reverse button handler

    /**
     *
     *  check on bluetooth adapter
     *
     */
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if(mBluetoothAdapter != null) {

        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
        }

        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                if (device.getName().equals("raspberrypi")) //Note, you will need to change this to match the name of your device
                {
                    Log.e("Robo", device.getName());
                    mmDevice = device;
                    break;
                }
            }
        }
    } else{

        // failed send the response to user
        (new Thread(new render("Error: could not find device \n"))).start();
    }
}
}

具有按钮应该调用的功能的类:

package com.example.michael.bluetoothcontroll;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

/**
  * responsible for all bluetooth threads
 */
public class blueToothControl {

BluetoothSocket mmSocket;
String connectionConfirmation;
String workConfirmation;

/**
 *
 * connect thread
 * to establish the bluetooth socket
 */
final class connectThread implements Runnable{

    BluetoothDevice mmDevice;

    public connectThread(BluetoothDevice Device){

        mmDevice = Device;
    }

    @Override
    public void run() {

        // try and connect to pi
        connectBT(mmDevice);
    }
}

/**
 *
 * workerThread
 * for sending data over the bluetooth socket
 *
 */
final class workerThread implements Runnable {

    private String btMsg;

    public workerThread(String msg) {

        // chars that get sent to the pi
        btMsg = msg;
    }

    public void run()
    {
        try {

            if(mmSocket != null) {

                OutputStream mmOutputStream = mmSocket.getOutputStream();
                mmOutputStream.write(btMsg.getBytes());
                workConfirmation = "sent \n >";

            }else{

                workConfirmation = "Error: failed to send" + btMsg + "\n > ";
            }

        } catch (IOException e) {

            e.printStackTrace();
        }

    }
}

/**
 * connectBT
 * @param mmDevice
 * takes the device and trys to create a socket with it
 */
protected void connectBT(BluetoothDevice mmDevice) {

    //UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
    final UUID uuid = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee"); //Standard SerialPortService ID


    try {

        if(mmDevice != null) {
            mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
            if (!mmSocket.isConnected()) {
                mmSocket.connect();
            }
        }else{

            connectionConfirmation = "Error: failed to connect to device \n >";
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

// this function never runs when I click
protected String callConnectThread(BluetoothDevice Device){

    (new Thread(new connectThread(Device))).start();

    return connectionConfirmation;
}

// this function never runs 
protected String callWorkThread(String msg){

    (new Thread(new workerThread(msg))).start();

    return workConfirmation;
}

}

我的主要活动的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".control"
android:clickable="true"
android:background="@color/cool_blue">

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="FWD"
    android:id="@+id/FWD"
    android:layout_above="@+id/Left"
    android:layout_centerHorizontal="true"
    android:width="100dp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="REV"
    android:id="@+id/REV"
    android:width="100dp"
    android:layout_below="@+id/Right"
    android:layout_centerHorizontal="true" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Left"
    android:id="@+id/Left"
    android:width="100dp"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/FWD"
    android:layout_toStartOf="@+id/FWD" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Right"
    android:id="@+id/Right"
    android:layout_below="@+id/FWD"
    android:layout_toRightOf="@+id/REV"
    android:width="100dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="start"
    android:id="@+id/settings"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="34dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="data"
    android:id="@+id/confirm"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:width="200dp"
    android:background="#ffffff"
    android:height="100dp"
    android:textColor="#000000" />

我认为这与不在同一个类中的功能有关,但是我可能会错了,任何帮助将不胜感激

您在哪里调用render

另外,在您的xml中,执行android:onclick = functionName,这是一种更好的方法,也是IMO更好的方法来确保调用按钮侦听器

您需要添加

android:onClick="whatevermethodthatbuttonisconnectedtoo" 

在您的.xml文件中。 我会先尝试将其设置为(视图视图)进行测试。

可以在我的问题下的评论中看到答案。 Mike.M建议只调用一次setContentView()并取出渲染线程。 当我这样做时,该应用程序开始工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM