1
public class DemoActivity extends FragmentActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);
}
TextView textView = (TextView) findViewById(R.id.demo1);
textview.setText("hi"); 

它不会识别 textView,然后我不能使用它的方法,所以我不能继续。可能会发生什么?

布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView 
    android:id="@+id/demo1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

 </LinearLayout>
4

1 回答 1

2

它不在onCreate()所以它不能识别,把它放进去onCreate()

public class DemoActivity extends FragmentActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);
    TextView textView = (TextView) findViewById(R.id.demo1);
    textview.setText("hi"); 

}
于 2013-07-21T00:58:22.660 回答