开发者

Android UI Beginner question

开发者 https://www.devze.com 2023-03-06 19:09 出处:网络
I\'m new to Android/java, coming from C#/Visual Studio. It\'s not a too hard jump from C# to java while coding the logic, but with the UI, I\'m having problems.

I'm new to Android/java, coming from C#/Visual Studio. It's not a too hard jump from C# to java while coding the logic, but with the UI, I'm having problems.

I've now added a simple TextView to my 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>

Now I wanted to access the textView1 by code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    this.textView1 = (TextView)this.findViewById(android.R.id.textView1);
}
private Tex开发者_如何学GotView textView1;

but I get the error: textView1 cannot be resolved.

What am I doing wrong?

Thanks

James


Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);

android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .

and set the background color to #ffffff in xml .... it seems more attractive..


Instead of android.R.id.textView1, just use R.id.textView1. The android prefix is for resources from Android itself, rather than from your project.


Replace findViewById(android.R.id.textView1) by findViewById(R.id.textView1);

android.R is resorce packege of SDK while R is of your app which will create along with successfully build of app .


try this

    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 !!!");
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消