0

我在这里看过几个类似的问题,但没有一个能解释我的问题。我只是想通过它的 id 来引用 TextView,但是每当我尝试对它做任何事情时,我都会不断收到空指针异常。我确保在两个 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");
    }

希望对某人有所帮助

4

3 回答 3

0

questionTextViewTextView 在fragment_main.xml布局中PlaceholderFragment,所以使用onCreateViewFragment 将 TextView 初始化为:

View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
TextView  mQuestionText = (TextView)rootView.findViewById(R.id.questionTextView);
于 2014-09-06T15:04:44.720 回答
0

如果你使用 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;
    }
} }
于 2014-09-06T15:07:42.923 回答
0

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

于 2014-09-06T15:08:43.447 回答