小编sai*_*eep的帖子

findViewById返回null(TextView)

我在谷歌以及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 …
Run Code Online (Sandbox Code Playgroud)

java android textview

3
推荐指数
1
解决办法
2895
查看次数

标签 统计

android ×1

java ×1

textview ×1