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 消息...每次都会抛出错误

最初它的前瞻性要严格得多,但是:/我在我的 R 文件中注意到,我的 hello world 字符串似乎没有像许多更改文本视图的示例中那样的 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>

4

1 回答 1

1

我建议一种不同的方法,在您的 TextView 中添加一个 id:

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

您可以使用以下方法更改其文本:

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

(此外,如果您的应用程序崩溃,您应该始终发布您的 LogCat,以便我们可以确切地看到正在发生的事情。)

于 2012-09-19T20:47:29.810 回答