setContentView后TextView为null

时间:2014-09-06 15:01:18

标签: java android nullpointerexception textview setcontentview

我在这里看了好几个类似的问题,但没有人解释过我的问题。我只是试图通过它的id引用TextView,但每当我尝试用它做任何事情时,我都会一直得到Null Pointer Exceptions。我确保在调用setContentView()并且片段膨胀后放置我的赋值。为了确保没有其他任何导致问题的原因,我删除了代码的其他部分并在分配后立即测试了null。无论如何,它仍然显示为空。

这是我的代码:

public class MainActivity extends Activity
{
    //MEMBER VARIABLES
    TextView mQuestionText;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null)
        {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        mQuestionText = (TextView) findViewById(R.id.questionTextView);
        if (mQuestionText == null)
            System.out.println("debug:: " + "null");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment
    {

        public PlaceholderFragment()
        {
        }

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

这里是fragment_main.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="edu.wichita.eecs.cs697ac.w387u669.assignment3.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/questionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:text="Question"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/questionTextView"
        android:text="False" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/questionTextView"
        android:layout_marginTop="35dp"
        android:layout_toLeftOf="@+id/questionTextView"
        android:text="True" />

</RelativeLayout>

感谢您的帮助!

编辑:在做了一些其他研究之后,我了解到我可以覆盖onStart()方法并将代码移到那里,因为我可以访问视图(甚至是片段中的视图)。然而,这只是一个旁路,并没有真正使用碎片,因为它们是要使用的。

protected void onStart()
    {
        super.onStart();
        TextView test = (TextView) findViewById(R.id.questionTextView);
        test.setText("hello");
    }

希望能帮到某人

3 个答案:

答案 0 :(得分:0)

questionTextView TextView位于fragment_main.xml的{​​{1}}布局中,因此请使用Fragment的PlaceholderFragment将TextView初始化为:

onCreateView

答案 1 :(得分:0)

如果你使用片段,你应该从片段OnCreateView方法

引用它
  

公共类MainActivity扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null)
    {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings)
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment
{

    public PlaceholderFragment()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
    TextView QuestionText = (TextView) rootView.findViewById(R.id.questionTextView);    
    if (mQuestionText == null)
        System.out.println("debug:: " + "null");
        return rootView;
    }
} }

答案 2 :(得分:0)

您已设置setContentView(R.layout.activity_main),但您的xml名称是fragment_main.xml 在setContentView中使用此XML,即setContentView(R.layout.fragment_main)