2012-09-19 52 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

相关问题