2011-05-19 155 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 ....似乎更具吸引力..