Button.setOnClickListener()在我的代码中不起作用

时间:2018-12-31 15:59:46

标签: android

我创建了一个按钮,并在该按钮上使用了setOnClickListner()方法。但是当我尝试运行并单击按钮时,它不起作用。

package com.example.administrator.sportsspeaker;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.ParseObject;
import com.parse.SaveCallback;

public class MainActivity extends AppCompatActivity  {
    private TextView textViewKickSpeed,textViewKickPower;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textViewKickSpeed = findViewById(R.id.textKickSpeed);
        textViewKickPower = findViewById(R.id.textKickPower);
        button = findViewById(R.id.submitbutton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("working","");
//                ParseObject kickBoxing = new ParseObject("KickBoxing");
//                kickBoxing.put("KickSpeed",""+textViewKickSpeed.getText());
//                kickBoxing.put("KickPower",""+textViewKickPower.getText());
//                kickBoxing.saveInBackground(new SaveCallback() {
//                    @Override
//                    public void done(ParseException e) {
//                        if(e == null){
//
//                            Toast.makeText(MainActivity.this,"Successfully uploaded ",Toast.LENGTH_LONG)
//                                    .show();
  //                      }
 //                   }
 //              });
            }
        });
    }

}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/textKickSpeed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="kickSpeed"
        android:inputType="text|number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/textKickPower"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="kickpower"
        android:inputType="number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textKickSpeed" />

    <Button
        android:id="@+id/submitbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="submit"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textKickPower" />

</android.support.constraint.ConstraintLayout>

错误代码:

2018-12-31 19:52:20.065 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:52:20.404 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:52:20.742 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:52:21.024 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:52:21.437 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:52:21.773 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:52:22.161 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:43.315 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:43.544 4605-4630/com.example.administrator.sportsspeaker E/EGL_emulation: tid 4630: eglSurfaceAttrib(1354): error 0x3009 (EGL_BAD_MATCH)
2018-12-31 19:53:43.604 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:43.887 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:44.180 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:44.525 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:44.906 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:45.254 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:45.633 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 19:53:45.960 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:11:11.549 521-551/system_process E/BluetoothAdapter: Bluetooth binder is null
2018-12-31 20:14:31.072 521-551/system_process E/BluetoothAdapter: Bluetooth binder is null
2018-12-31 20:15:10.026 320-342/? E/BandwidthController: Updating quota globalAlert failed (Status[code: 2, msg: "[No such file or directory] : fopen("/proc/net/xt_quota/globalAlert", "we") failed"])
2018-12-31 20:18:37.550 521-551/system_process E/BluetoothAdapter: Bluetooth binder is null
2018-12-31 20:18:37.578 424-441/? E/OMXNodeInstance: setConfig(0xf192c960:google.vorbis.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001)
2018-12-31 20:18:37.579 424-441/? E/OMXNodeInstance: getConfig(0xf192c960:google.vorbis.decoder, ConfigAndroidVendorExtension(0x6f100004)) ERROR: Undefined(0x80001001)
2018-12-31 20:18:37.905 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:38.223 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:38.495 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:38.822 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:39.165 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:39.487 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:39.784 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:40.052 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:40.389 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:40.723 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:41.048 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:18:41.315 379-405/? E/AudioStreamOutSink: Error while writing data to HAL: -61
2018-12-31 20:32:59.553 521-551/system_process E/BluetoothAdapter: Bluetooth binder is null

2 个答案:

答案 0 :(得分:1)

从您的代码中,我可以看到您已使用Log.i()打印了信息日志,但是从您的日志中可以清楚地看到,您仅在 logcat Error 日志>。

您可以将日志视图类型在 logcat 中更改为信息,或将日志打印更改为Log.e()

您也没有将任何日志消息放入日志打印中,这不是强制性的,但很有帮助。您在方法“工作” 中编写的第一部分不是消息,而只是标签。消息可以这样写:

Log.i("working", "your log message here");

答案 1 :(得分:0)

代替使用

Log.i("working","");

尝试

Toast.makeText(MainActivity.this,"Button Pressed ",Toast.LENGTH_SHORT).show();

这样,您不必依赖日志。

我知道这不能回答您的问题,但是将来可以帮助您进行调试

关于回答您的问题,请尝试在生命周期中稍后设置点击侦听器,例如在onStart()中