繁体   English   中英

Android:TextView导致应用崩溃

[英]Android: TextView Causing App to Crash

我试图让一个TextView根据条件显示一个字符串。 但是,当我尝试实现它时,它导致应用程序停止工作。 这是相关的代码/ 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)是我编写的一种方法,该方法从给定索引的数组中返回整数。

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" />

为什么会崩溃?

编辑*这是LogCat:

在此处输入图片说明

我不确定您需要多少。

如果您的代码良好且一致,则button = button和textview = textview,请清理您的项目。 我已经看到通过在Eclipse中使用clean函数解决了此类问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM