按钮不响应点击。

3
我创建了一个简单的活动,旨在向另一个活动发送两个信息。然而,我的按钮没有注册任何点击事件。有人知道为什么吗?我预计通过在XML文件中添加onClick =“ onClickWrite”来链接它们,但是当单击时没有响应。如果有人能帮助我,我将非常感激。我尝试实现onClickListeners,但是使用该实现会在我的Toast上引发错误。 活动
public class InitializeClass extends Activity {

    private Service service;
    private Button button;
    EditText infoOne; 
    EditText infoTwo; 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_control);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(clicked);
        infoOne= (EditText) findViewById(R.id.editText);
        infoTwo= (EditText) findViewById(R.id.editText1);

    }

    private View.OnClickListener clicked = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if (service != null) {

                int size = 100;
                byte[] byteArray = new byte[size];
                byte[] byteArrayTwo = new byte[size];
                byteArray = infoOne.getText().toString().getBytes(Charset.defaultCharset());
                byteArrayTwo= infoTwo.getText().toString().getBytes(Charset.defaultCharset());

                if ((!(infoOne.getText().toString().isEmpty())) && (!(infoTwo.getText().toString().isEmpty()))) {
                    service.setInfo(byteArray);
                    service.setInfoTwo(byteArrayTwo);
                    intentMethod();
                }
            }
        }
    };

    public void intentMethod() {
        Intent intent = new Intent(this, DeviceScanActivity.class);
        startActivity(intent);
    }
}

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"
    tools:context=".InitializeClass">


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/send_info"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="@color/white"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText1" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="150dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColorHint="@android:color/white"
        android:textCursorDrawable="@drawable/color_cursor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="info"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColorHint="@android:color/white"
        android:textCursorDrawable="@drawable/color_cursor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

</android.support.constraint.ConstraintLayout>

你的Java类名与XML文件不匹配。 - karthik
@LostandConfused 我认为是这样,你有任何错误消息或警告吗(请参见LogCat)?你尝试过仅在Java中实现它吗?那样行吗? - deHaar
@deHaar,我的LogCat中没有错误或警告,并且我已经按照以下建议仅使用Java进行了实现,但是没有成功。我会更新我的代码。 - LostandConfused
也许你需要先初始化“Button”?我不太确定是否有必要,而且现在无法测试它... - deHaar
@deHaar 我在我的更新版本中实际上已经做到了这一点。 - LostandConfused
显示剩余3条评论
5个回答

3

您需要先初始化您的按钮。

Button button = (Button) findViewById(R.id.button);

希望这可以帮到你!

很遗憾,我在这方面没有什么运气。我更新了我的代码,以便我用Java来实现它。你还有其他想法或者看到的错误吗? - LostandConfused

1
在你的Activity类中,将按钮分配给xml中按钮的id:
private Button btn = findViewById(R.id.button);

您将创建一个 OnClickListener:
    private View.OnClickListener clicked = new View.OnClickListener(){
    @Override
    public void onClick(View view) {
        //Insert Code here
    }
};

在同一活动类中的onCreate方法中,您将实例化按钮并将按钮分配给onClick事件监听器:
btn = new Button(this);
btn.setOnClickListener(clicked);

这也可以使用XML完成,但是我认为最好只使用XML来处理UI,而不是功能。 - nativeAndroidCodes
很遗憾,我在这方面没有什么运气。我更新了我的代码,以便我用Java来实现它。你还有其他想法或者看到的错误吗? - LostandConfused

1

你需要完成以下几个步骤:

  1. 创建字段。

    private Button button;

  2. 初始化按钮。

    Button button = (Button) findViewById(R.id.button);

  3. 在按钮上设置监听器。

    button.setOnClickListener();


很遗憾,我在这方面没有什么运气。我更新了我的代码,以便我用Java来实现它。你还有其他想法或者看到的错误吗? - LostandConfused

0

你需要在Java代码中将按钮(名称)进行转换。 例如,在xml上,你有id/button(名称) 在Java文件中声明它,并进行转换。 onClickListner将如下所示:

转换按钮:Button button;

Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

0

我找到答案了。在if (service != null)中的服务从未被初始化。这是我的一个愚蠢错误。感谢大家的帮助和指导,让我能够实现onClickListener!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接