如果自定义 Textview 背景,应用程序会崩溃
我正在制作一个应用程序,单击按钮后它会更改为不同的布局。一切正常,但是当我在第二个布局的背景中设置 textView 的样式时,它会崩溃,并且在 Eclipse 上显示“未找到源”
这是未崩溃时 textview 的 XML:
<TextView android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content" android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" android:layout_marginTop="15dp"
android:id="@+id/licenseKey" android:typeface="monospace"
android:text="@string/loading" />
这是崩溃时的 XML:
<TextView android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content" android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" android:layout_marginTop="15dp"
android:id="@+id/licenseKey" android:typeface="monospace"
android:text="@string/loading" android:background="@string/grey"/>
这是活动代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
TextView licenseKey = (TextView)findViewById(R.id.licenseKey);
licenseKey.setText(generateKey());
}
public String generateKey() {
return "D5JO4-7VQP8-D7B3X-L8N0D";
}
我对问题出在哪里感到非常困惑。文本视图通常显示假许可证代码,但一旦我更改背景,它就会崩溃。我什至看不到第二个布局。在我的 Nexus S Android 2.3.3 上进行调试。
I am making an application, after clicking a button it changes to a different layout. All works well, but when I style the textView in the 2nd layout's background, it crashes and on eclipse says "Source not found"
Here is the XML of the textview when it doesnt crash:
<TextView android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content" android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" android:layout_marginTop="15dp"
android:id="@+id/licenseKey" android:typeface="monospace"
android:text="@string/loading" />
Here it is when it crashes:
<TextView android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content" android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" android:layout_marginTop="15dp"
android:id="@+id/licenseKey" android:typeface="monospace"
android:text="@string/loading" android:background="@string/grey"/>
Here is the activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
TextView licenseKey = (TextView)findViewById(R.id.licenseKey);
licenseKey.setText(generateKey());
}
public String generateKey() {
return "D5JO4-7VQP8-D7B3X-L8N0D";
}
I am seriously stumped as to what the problem is. The textview normally shows the fake license code but once i change the background it crashes. I dont get to even see the 2nd layout. Debugging on my Nexus S Android 2.3.3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将 android:background 属性设置为可绘制或 rgb 值,而不是字符串。请参阅此处。
例如:
android:background="#bebebe"
。You should set the android:background attribute to a drawable or a rgb value, not a string. See here.
For example:
android:background="#bebebe"
.难道不应该是这样吗
Shouldn't that be