android - Fragment 中 EditText 小部件上的 NullPointerException

标签 android nullpointerexception android-edittext

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

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

我假设这是因为“ react 物​​”EditText 小部件在其声明中仍然为 null,但我不明白为什么,因为我在 onCreateView() 方法中声明了它。任何有关我应该如何解决此问题的帮助将不胜感激。

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

Java

    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() 方法之前执行被执行,这是你膨胀持有这个 View 的布局的地方。在膨胀之前,您无法访问 View 来设置/获取其内容。解决方案是在这些行之后的某个时间调用 String reactant = reactants.getText().toString();:

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

当然,如果您没有输入任何文本,该字符串将为空,但这至少可以让您合法地检查而不会出现 NullPointerException。

关于android - Fragment 中 EditText 小部件上的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37929116/

相关文章:

android - 在某些手机上,文本不会出现在编辑文本中

android - TextView 和 EditText 在 LinearLayout 中被截断

android - Google Analytics 'Mobile Device Model' 和 Android 的 Build.MODEL

java - PlaceAutoCompleteFragment setHint NPE

java - 查询sqlite数据库出错

java - 对包含空元素的数组进行排序

android - EditText 上的 KeyListener : Android

android - 无法将项目导入 Eclipse 工作区

android - Youtube 视频不会在带有 View 寻呼机的 fragment 内的 WebView 中停止

安卓应用结构