2011-05-19 158 views
0

我是新来的Android/java,来自C#/ Visual Studio。 编写逻辑时,从C#跳转到java并不是太困难,但使用UI时,我遇到了问题。Android UI初学者问题

我现在已经添加了一个简单的TextView我的main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:orientation="vertical"> 
<TextView android:layout_width="wrap_content" android:editable="true" android:layout_height="wrap_content" android:id="@+id/textView1" android:text="TextView"> 
</TextView> 
</LinearLayout> 

现在我想通过代码来访问textView1:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    this.textView1 = (TextView)this.findViewById(android.R.id.textView1); 
} 
private TextView textView1; 

,但我得到的错误:textView1不能解决。

我在做什么错?

感谢

詹姆斯

回答

1

相反的android.R.id.textView1,只需使用R.id.textView1android前缀是针对Android本身的资源,而不是来自您的项目。

+0

将id更改为另一个 – Jone 2012-11-20 09:05:45

1

通过SDK的findViewById(R.id.textView1);

android.R是resorce packege更换findViewById(android.R.id.textView1)而R是你的应用程序,它会随着成功构建应用程序的创建。

1

试试这个

private TextView textView1; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     this.textView1 = (TextView)this.findViewById(R.id.textView1); 
     textView1.setText("Hello World !!!"); 
    } 
2

更换findViewById(android.R.id.textView1)通过findViewById(R.id.textView1);

android.R是resorce SDK的packege,而R是你的应用程序,它将随着应用程序的成功构建而创建。

并将背景颜色设置为xff中的#ffffff ....似乎更具吸引力..