我是Android / java新手,来自C#/ Visual Studio。 在编写逻辑时,从C#跳到java并不是太难,但是使用UI,我遇到了问题。
我现在已经在main.xml中添加了一个简单的TextView
<?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。
我做错了什么?
由于
詹姆斯
答案 0 :(得分:2)
用findViewById(R.id.textView1)替换findViewById(android.R.id.textView1);
android.R是SDK的resorce packege,而R是你的应用程序,它将与成功构建应用程序一起创建。
并将背景颜色设置为xml中的#ffffff ......看起来更具吸引力......
答案 1 :(得分:1)
而不是android.R.id.textView1
,只需使用R.id.textView1
。 android
前缀适用于Android本身的资源,而不是来自您的项目。
答案 2 :(得分:1)
将findViewById(android.R.id.textView1)
替换为findViewById(R.id.textView1);
android.R是SDK的资源包,而R是你的应用程序,它将与成功构建应用程序一起创建。
答案 3 :(得分: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 !!!");
}