求大神~关于判断String.xml是否存在某个value的问题 如题,获取String.xml里面的资源的代码是Context.getString(R.string.xxx),但我想在获取之前做一次判断,判断xxx这个value是否存在于String.xml里面,求各位大神的解决办法~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将你的View aboutbutton ... 改成Button aboutbutton 很简单 TextView tx = new TextView(); tx.setText(R.String.XXX); 判断tx.getText()是否为空即可.... 不行啊,改成Button后直接就红叉了。报错说是Button Cannot be resolved to a type 后来加了这样一个import android.widget.Button;不报错了。可是依然运行不出来,日志中还是一摸一样的错误。。 通过Button b = (Button)findViewById(R.id.about_button); 获得Button对象 MainActivity及需要跳转的AboutActivity 都需要在mainfest清单文件中申明 已经申明过了。这是我AndroidMainfest.xml文件。其中有两个Activity,第二个就是要跳转的Activity。<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloandroid" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/activity_main_title" android:theme="@style/AppTheme" > <activity android:name="com.example.helloandroid.MainActivity" android:label="@string/continue_lable" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.helloandroid.AboutActivity" android:label="@string/about_title" > </activity> </application></manifest> try { context.getString(R.string.xxx);} catch (Exception e) { // no resource found.} 问题是如果string.xml里没有定义这个xxx字符串,在R文件里面是找不到这个id的啊,也就是eclipse会在R.string.xxx里面报错呀 哎呀...忘了 不存在 R.java文件就不会生成id值 编译是不会通过的 android framebuffer 截屏操作问题 在布局文件放一个控件,然后引用跟直接代码实例控件有什么不同 如何实现多个应用程序间的调用 Andriod模拟机运行时显示很大怎样调整 各类资源0分下载---第四期 androi的布局标签说明文档 如何动态变化菜单的标题与图像? android 程序状态控制 一个简单的service, OnStartCommand怎么不能执行? 关于android sdk更新到4.4.2后 android发送带中文的httpost请求乱码问题 为什么我下载别人的android代码运行却又错误,空指针
改成Button aboutbutton
tx.setText(R.String.XXX); 判断tx.getText()是否为空即可....
通过Button b = (Button)findViewById(R.id.about_button); 获得Button对象
MainActivity及需要跳转的AboutActivity 都需要在mainfest清单文件中申明
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/activity_main_title"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.helloandroid.MainActivity"
android:label="@string/continue_lable" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.helloandroid.AboutActivity"
android:label="@string/about_title" >
</activity>
</application></manifest>
context.getString(R.string.xxx);
}
catch (Exception e) {
// no resource found.
}