findViewById 返回 null (TextView)

3
我已经在谷歌和StackOverflow上搜索过了,但是没有找到解决我的问题的方法。 当我在onCreate函数中使用findViewById函数时,它返回null。 但是,当我将它更改为onClick函数时,它正常工作。 请帮助我,因为我是Android编程的初学者。
我的MainFunction.java:
public class MainActivity extends ActionBarActivity {

@Override
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();
    }
    final TextView t = (TextView) findViewById(R.id.textView1);
    t.setText("hi world.");

}

@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

<LinearLayout 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.test.MainActivity$PlaceholderFragment" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="onClick" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="108dp"
    android:layout_marginTop="68dp"
    android:text="TextView" />


你正在使用 setContentView(R.layout.activity_main); 并且 textView1fragment_main 中。将代码移动到你的 Fragment 中。 - Apoorv
2个回答

5

您的TextView位于片段(fragment_main.xml)中,而您正在查看R.layout.activity_main。因此最好在您的Fragment中(即PlaceholderFragment)编写findViewById。或者您可以使用Inflater获取您的fragment_layout,然后使用view.findViewById获取您的TextView


4
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    TextView t = (TextView) rootView.findViewById(R.id.textView1);
    t.setText("hi world.");
    return rootView;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接