简体   繁体   English

在TextView中设置文本会引发空指针异常

[英]Setting text in TextView throws a null pointer exception

I am just starting out android programming, and I ran into this error while trying to set the text in a TextView. 我刚刚开始进行android编程,尝试在TextView中设置文本时遇到了此错误。 I have researched other people's problems, but I have seemed to take care of the trouble issues that were pointed out by their answers. 我已经研究了其他人的问题,但是我似乎已经在照顾他们的答案所指出的麻烦问题。

Here is my XML Code: 这是我的XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.everythingmath.MainActivity$PlaceholderFragment" >

<GridLayout
    android:id="@+id/problemLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:columnCount="5"
    android:rowCount="1" >

    <TextView 
        android:id="@+id/firstNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Number"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" + "
        android:textSize="30sp" />

    <TextView 
        android:id="@+id/secondNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second Number"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/numTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number" 
        android:hint="Text Goes Here"
        android:textSize="20sp"/>

</GridLayout>
</RelativeLayout>

Here is my pertinent Java: 这是我相关的Java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    int firstNumber = createRandomNumber(10);
    int secondNumber = createRandomNumber(10);
    TextView firstNumText = (TextView)findViewById(R.id.firstNumber);
    TextView secondNumText = (TextView)findViewById(R.id.secondNumber);
    firstNumText.setText(Integer.toString(firstNumber));
    secondNumText.setText(Integer.toString(secondNumber));
}

It is the lines in which I try to set the text that throw the error. 在这些行中,我尝试设置引发错误的文本。 The random number generator works fine also. 随机数生成器也可以正常工作。

Localize PlaceholderFragment class inside MainActivity class. MainActivity类中本地化PlaceholderFragment类。

Change public View onCreateView method into public View onCreateView方法更改为

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    int firstNumber = createRandomNumber(10);
    int secondNumber = createRandomNumber(10);
    TextView firstNumText = (TextView)findViewById(R.id.firstNumber);
    TextView secondNumText = (TextView)findViewById(R.id.secondNumber);
    firstNumText.setText(Integer.toString(firstNumber));
    secondNumText.setText(Integer.toString(secondNumber));

    return rootView;
}

You will probably need to move createRandomNumber method to PlaceholderFragment class. 您可能需要将createRandomNumber方法移动到PlaceholderFragment类。 And in case you have changed Fragment's name from fragment_main to something else, change it also in this line 如果您已将Fragment的名称从fragment_main更改为其他名称,请在此行中也将其更改

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

Your current code doesn't work because inside 您当前的代码无效,因为

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    [..]
}

you can only find by ID view ( findViewById ) which was declared inside activity_main layout. 您只能通过在activity_main布局中声明的ID视图( findViewById )查找。 And your TextView views are probably declared inside fragment_main layout. 而且您的TextView视图可能在fragment_main布局内声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM