java - findViewById 返回 null (TextView)

标签 java android textview

<分区>

我在 google 和 StackOverflow 上搜索过。 但是没有一个解决方案解决了我的问题。 findViewById 在 onCreate 中时返回 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" />

最佳答案

您的 TextView 位于 fragment (fragment_main.xml) 中,您正在查看 R.layout.activity_main。所以最好这样写findViewById 在你的 Fragment 中,它是 PlaceholderFragment。或者你可以使用 Inflater 来获取你的 fragment_layout 而不是使用 view.findViewById 获取您的 TextView

关于java - findViewById 返回 null (TextView),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24383387/

相关文章:

java - 用java流式传输音乐

java - 使用 JApplet 获取图像

java - 如何分隔数组开头的名称?

Android缩放比例覆盖在 map View 上的缩放比例等

android - Android : a simple example 上的自定义 toast

java - Netbeans 7,如何设置 Maven 的 JDK 路径?

默认锁定屏幕上的 Android Activity

android - 在 android 文本切换器或 View 翻转器中将大文本分成页面

android - 在 AlertDialog 中更改 textView 文本时出现 NullPointerException

android TextView setText 不工作