Java 应用程序在单击按钮时崩溃

Java 应用程序在单击按钮时崩溃,java,android,Java,Android,我正在尝试构建自定义数字选择器,每次单击+或-按钮时,我的应用程序都会崩溃。我在MainActivity.java中没有收到任何错误。 有人知道会是什么情况吗? 代码如下: public class MainActivity extends Activity { TextView tvHeight; int counter = 0; @Override protected void onCreate(Bundle savedInstanceState) {

我正在尝试构建自定义数字选择器,每次单击+或-按钮时,我的应用程序都会崩溃。我在MainActivity.java中没有收到任何错误。 有人知道会是什么情况吗? 代码如下:

public class MainActivity extends Activity {

    TextView tvHeight;
    int counter = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button heightMinus = (Button) findViewById(R.id.height_min);
        Button heightPlus = (Button) findViewById(R.id.height_plus);
        tvHeight = (TextView) findViewById(R.id.textViewHeight);

        heightPlus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                counter++;
                tvHeight.setText(counter);

            }
        });

        heightMinus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                counter--;
                tvHeight.setText(counter);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
您需要调用需要
CharSequence
作为参数的方法。因此,替换

tvHeight.setText(counter);

当前您正在调用
setText(intresid)
,它将尝试查找您在
strings.xml
文件中定义的字符串资源id

因此,我猜您的代码抛出了一个
ResourceNotFoundException

,您需要调用一个方法,该方法需要一个
CharSequence
作为参数。因此,替换

tvHeight.setText(counter);

当前您正在调用
setText(intresid)
,它将尝试查找您在
strings.xml
文件中定义的字符串资源id

所以我猜你的代码抛出了一个
ResourceNotFoundException

改变这个

tvHeight.setText(counter);

看看这些方法

public final void setText (int resid)
还有这个

public final void setText (CharSequence text)
使用第一种方法,Android会查找id为meted的资源,如果找不到,则会获取
ResourceNotFoundException
。有一个使用int作为资源Id,另一个使用charactersequencne更改此设置

tvHeight.setText(counter);

看看这些方法

public final void setText (int resid)
还有这个

public final void setText (CharSequence text)

使用第一种方法,Android会查找id为meted的资源,如果找不到,则会获取
ResourceNotFoundException
。有一个接受int,它是资源Id,另一个接受CharacterSequecne

你看了logcat输出了吗?为什么你把它标记到
numberpicker
?你看了logcat输出了吗?为什么你把它标记到
numberpicker
?非常感谢。这很有帮助,非常感谢。这很有帮助。