[英]Android: TextView Causing App to Crash
I am trying to get a TextView to display a string depending on a condition. 我试图让一个TextView根据条件显示一个字符串。 However, when I tried to implement it, it causes the app to stop working.
但是,当我尝试实现它时,它导致应用程序停止工作。 Here is the relevant code/xml:
这是相关的代码/ xml:
// class declaration, etc.
TextView textString;
// other code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
...
textString = (TextView) findViewById(R.id.textId);
...
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_id:
...
if(getNum(0) == 10) {
textString = (TextView) findViewById(R.id.textId);
textString.setText("String 1");
}
if(getNum(1) == 10) {
textString = (TextView) findViewById(R.id.textId);
textString.setText("String 2");
}
// etc.
getNum(int index) is a method I wrote that returns an integer from an array given an index. getNum(int index)是我编写的一种方法,该方法从给定索引的数组中返回整数。
XML Code for TextView: TextView的XML代码:
<TextView
android:id="@+id/textId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/other_id"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
Why is it crashing? 为什么会崩溃?
Edit* Here is the the LogCat: 编辑*这是LogCat:
I am not sure how much of it you need. 我不确定您需要多少。
IF your code is good and consistent, button = button and textview = textview, clean your project. 如果您的代码良好且一致,则button = button和textview = textview,请清理您的项目。 I've seen issues like this resolved by using the clean function in eclipse.
我已经看到通过在Eclipse中使用clean函数解决了此类问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.