2012-09-19 54 views
0
int resID = getResources().getIdentifier(getResources().getString(R.string.hello_world), "id", "com.example.test.projects"); 
     TextView text = (TextView)findViewById(resID); 
     text.setText("cpu: "); 

我似乎無法改變程序hello_world消息...它拋出錯誤每更改TextView?當我嘗試它拋出錯誤

本來這是一個很多兩岸前瞻性,但時間:我注意到/在我的R檔即我對你好世界didnt串似乎有一個ID像許多改變文本視圖的例子,所以我的繼承人扔了一起

在那裏我做了佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/hello_world" 
    tools:context=".MainActivity" /> 

和我的主XML:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.test.projects" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="7" 
    android:targetSdkVersion="16" /> 
<uses-feature android:name="android.hardware.bluetooth"/> 
<uses-permission android:name="android.permission.WRITE_SETTINGS"/> 
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> 
<uses-permission android:name="android.permission.BLUETOOTH"/> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

+0

你不想要一個按鈕來改變文本嗎? –

回答

1

我提出一個不同的方法,添加ID到您的TextView:

<TextView 
    android:id="@+id/helloTextView" 
    ... /> 

你可以改變它的文字:

TextView text = (TextView) findViewById(R.id.helloTextView); 
text.setText("cpu: "); 

(同樣如果你的應用程序崩潰,你應該永遠po st你的LogCat,所以我們可以看到到底發生了什麼。)

+0

謝謝,我試過類似的東西,但沒有包括@ + ID/ – Danger

+0

沒有麻煩!你可能知道這一點,但你可以通過改變'android:text =「」'屬性來更改應用程序開始的文本_before_。 – Sam

相關問題