【问题标题】:Fragment and onClick Button [duplicate]片段和onClick按钮[重复]
【发布时间】:2017-10-28 07:13:19
【问题描述】:

我正在尝试为片段内的按钮 (R.id.addclick) 设置 onclick 方法。没有错误,但每次我启动模拟器并按下按钮时,它一直显示“不幸的是,...已停止”。请给我一些建议。

first_layout xml:

Button
        android:id="@+id/addclick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit17"
        android:layout_marginTop="16dp"
        android:layout_toEndOf="@+id/textView32"
        android:layout_toRightOf="@+id/textView32"
        android:onClick="addclick"
        android:text="submit" />

我的整个片段代码:

package com.example.administrator.realrandd;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Administrator on 2017-10-28.
 */
public class FirstLayout extends Fragment implements View.OnClickListener {
    View v;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v = inflater.inflate(R.layout.first_layout, container, false);

        Button b = (Button) v.findViewById(R.id.addclick);
        b.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addclick:

                EditText number1 = (EditText)v.findViewById(R.id.edit1);
                EditText number2 = (EditText)v.findViewById(R.id.edit2);

                TextView result = (TextView)v.findViewById(R.id.Final);

                int n1 = Integer.parseInt(number1.getText().toString());
                int n2 = Integer.parseInt(number2.getText().toString());

                result.setText(Integer.toString(n1+n2));



                break;
        }
    }
}

【问题讨论】:

  • 从您的 xml 中删除 android:onClick="addclick" 并检查。如果您仍然遇到任何错误,请分享日志

标签: java android android-fragments


【解决方案1】:

名称不匹配:在您的布局中,您指的是一个名为

的方法
android:onClick="addclick"

但在您的代码中,您设置了一个名为

的方法
public void onClick(View v) {

和... "addclick" != "onClick"

您应该使这两个名称匹配。


因此,在您的代码中为您的方法命名:

public void addclick(View v) {

【讨论】:

  • 先生,android:onClick="onClick" 在 FRAGMENT 中不起作用
  • @IntelliJAmiya 已更正。谢谢你,先生。
【解决方案2】:

我认为下面的文本字段是空的。这就是为什么空字符串不能转换为 int 的原因。

EditText number1 = (EditText)v.findViewById(R.id.edit1);
EditText number2 = (EditText)v.findViewById(R.id.edit2);

在这种情况下,如果您查看日志,您会发现类似

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)

错误

【讨论】:

  • 如果您在日志中发现任何其他异常。请提供
猜你喜欢
  • 1970-01-01
  • 2016-07-28
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 2018-10-08
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多