【问题标题】:TextView is null after setContentView在 setContentView 之后 TextView 为空
【发布时间】:2014-09-06 15:01:18
【问题描述】:

我在这里查看了几个类似的问题,但没有一个能解释我的问题。我只是想通过它的 id 来引用 TextView,但是每当我尝试对它做任何事情时,我都会不断收到空指针异常。我确保在 setContentView() 都被调用并且片段被膨胀之后进行分配。为确保没有其他问题导致问题,我删除了代码的其他部分并在分配后立即测试 null。不管怎样,它仍然显示为 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");
    }

希望对某人有所帮助

【问题讨论】:

    标签: java android nullpointerexception textview setcontentview


    【解决方案1】:

    questionTextViewTextView在PlaceholderFragmentfragment_main.xml布局中,所以使用Fragment的onCreateView将TextView初始化为:

    View rootView = inflater.inflate(R.layout.fragment_main, container,
                        false);
    TextView  mQuestionText = (TextView)rootView.findViewById(R.id.questionTextView);
    

    【讨论】:

    • 成功了!认为这可能是问题所在,但担心必须将所有内容声明为静态才能使其正常工作。结果不是问题。再次感谢
    【解决方案2】:

    如果你使用 Fragment,你应该从 Fragment 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;
        }
    } }
    

    【讨论】:

    • 感谢您的回答。这最终成为了问题,但 ρяσѕρєя K 先回答了,所以我把它给了他们
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 1970-01-01
      • 2018-09-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 2013-01-07
      相关资源
      最近更新 更多