我是Android App Dev的新手,所以错误可能真的很简单,但是我的代码在此行代码中抛出了NullPointerException

    String reactant = reactants.getText().toString();


我以为这是因为“ reactants” EditText小部件在其声明中仍为null,但我不明白为什么,因为我在onCreateView()方法中对其进行了声明。我应该如何解决此问题的任何帮助将不胜感激。

这是我的Java和布局XML代码。

爪哇

    public class stoich_fragment extends Fragment
    {
    View rootview;
    int i = 0;
    int l = 0;
    ArrayList<Integer> arr = new ArrayList<>();
    ArrayList<String> elements = new ArrayList<>();
    boolean getElements = true;
    String s = "";
    String element = "";
    EditText reactants;
    TextView beq;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        rootview = inflater.inflate(R.layout.stoich_layout, container, false);
        reactants = (EditText) rootview.findViewById(R.id.reactants);
        beq = (TextView) rootview.findViewById(R.id.balanced_equation);
        return rootview;
    }
    String reactant = reactants.getText().toString();
    //saying that reactants is null even after it went through the onCreateView method
    String re = reactant.replaceAll("\\s+","");
    public void getReactants()
    {
        while(getElements)
        {
            String let = re.substring(i, i+1);
            if(let.compareTo(let.toLowerCase()) > 0)
            {
                element += let;
                i++;
            }
            else if(let.compareTo(let.toLowerCase()) == 0)
            {
                element += let;
                i++;
            }
            else if (let.equals("0")||let.equals("1")||let.equals("2")||let.equals("3")||let.equals("4")||let.equals("5")||let.equals("6")||let.equals("7")||let.equals("8")||let.equals("9"))
            {
                int temp = Integer.parseInt(let);
                arr.add(temp);
                elements.add(element);
                element = "";
            }
            else if(i > re.length()-1)
            {
                getElements = false;
            }
        }
        // displays the elements isolated on the reactant side
        // to test to make sure my logic works
        for(int a = 0; a<re.length(); a++)
        {
            s += elements.get(a) + " : " + arr.get(a) + "\n";
        }
        beq.setText(s);
    }
    }


XML格式

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"        android:layout_height="match_parent">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/reactants"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="95dp"
        android:textSize="20sp"
        android:inputType="text" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/products"
        android:layout_alignBottom="@+id/reactants"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="20sp"
        android:inputType="text" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/balanced_equation"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:textSize="30sp" />
<!--should make text bold and black-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Balancing Equations"
        android:id="@+id/title"
        android:textSize="35sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

最佳答案

您在任何方法之外声明String reactant = reactants.getText().toString();,这意味着它将在执行onCreateView()方法之前执行,这是在您膨胀容纳该视图的布局的地方。放大视图之前,您无法访问视图以设置/获取其内容。解决方案是在以下几行之后的某个时间调用String reactant = reactants.getText().toString();

rootview = inflater.inflate(R.layout.stoich_layout, container, false);
reactants = (EditText) rootview.findViewById(R.id.reactants);


当然,如果您未输入任何文本,则字符串将为空,但这至少可以使您合法地检查而不会得到NullPointerException。

10-08 02:24