当我尝试在片段上查找viewByid时为空对象

时间:2017-06-03 03:42:49

标签: android fragment

我在使用活动时这些代码工作正常。但是在我将其更改为使用Fragment后,它只是告诉我Enter是空对象,因此我无法在其中添加onClickListener

**更新: 我尝试使用getView()viewgetActivity()。但它们都不适合我。 **

有代码:

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

import com.maichongju.calculator.R;

import java.math.BigInteger;

public class Addition_Fragment extends Fragment{
public static final String TAG = "TAG";

private Button Enter; // The Enter Button
private EditText Number1;   // Top Number EditText
private EditText Number2;   // Bot Number EditText
private TextView ResultView;    // Result View

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for thlis fragment
    View view = inflater.inflate(R.layout.fragment_addition_,container,false);
    setup();
    return view;
}
private void setup() {
    define();
    Enter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String n1 = Number1.getText().toString().trim();
            String n2 = Number2.getText().toString().trim();
            if (n1.length() == 0 || n2.length() == 0) {
                if (n1.length() == 0 && n2.length() == 0) {
                    ResultView.setText(getString(R.string.Result_Text_Default));
                } else if (n1.length() == 0) {
                    ResultView.setText(getString(R.string.Result_Text_Number1_Missing));
                } else {
                    ResultView.setText(getString(R.string.Result_Text_Number2_Missing));
                }

            } else {
                BigInteger result = Calculation(n1, n2);
                ResultView.setText(n1 + " + " + n2 + " = " + result.toString());
            }

        }
    });
}

private BigInteger Calculation(final String s1, final String s2) {
    final BigInteger n1 = new BigInteger(s1);
    final BigInteger n2 = new BigInteger(s2);
    return n1.add(n2);
}

private void define() {
    Enter = (Button) getActivity().findViewById(R.id.Enter_Button);
    Number1 = (EditText) getActivity().findViewById(R.id.Number_EditText1);
    Number2 = (EditText) getActivity().findViewById(R.id.Number_EditText2);
    ResultView = (TextView) getActivity().findViewById(R.id.ResultTextView);
}

}

这是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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/Number_EditText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:ems="10"
        android:hint="@string/Number_Filed_Text"
        android:inputType="number"
        android:textAlignment="center"
        android:theme="@style/EditTextTheme"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/Number_EditText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="@string/Number_Filed_Text"
        android:inputType="number"
        android:textAlignment="center"
        android:theme="@style/EditTextTheme"
        app:layout_constraintLeft_toLeftOf="@+id/Number_EditText1"
        app:layout_constraintRight_toRightOf="@+id/Number_EditText1"
        app:layout_constraintTop_toBottomOf="@+id/Number_EditText1" />

    <TextView
        android:id="@+id/ResultTextView"
        android:layout_width="250dp"
        android:layout_height="127dp"
        android:layout_marginBottom="16dp"
        android:text="@string/Result_Text_Default"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="@+id/Enter_Button"
        app:layout_constraintRight_toRightOf="@+id/Enter_Button"
        app:layout_constraintTop_toBottomOf="@+id/Enter_Button"
        app:layout_constraintVertical_bias="0.25" />

    <Button
        android:id="@+id/Enter_Button"
        android:layout_width="138dp"
        android:layout_height="49dp"
        android:layout_marginTop="32dp"
        android:text="@string/Calculate_Button_Name"
        android:textAllCaps="false"
        android:textColor="@color/White"
        android:theme="@style/Button_Blue"
        app:layout_constraintLeft_toLeftOf="@+id/Number_EditText2"
        app:layout_constraintRight_toRightOf="@+id/Number_EditText2"
        app:layout_constraintTop_toBottomOf="@+id/Number_EditText2" />
</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

然后改变这个:

Enter = (Button) getActivity().findViewById(R.id.Enter_Button);

到此:

Enter = (Button) view.findViewById(R.id.Enter_Button);

不要忘记在主要功能

之外声明视图

我希望这会有所帮助