简体   繁体   English

按下按钮时应用程式停止运作

[英]App stops working when pressing a button

So I am starting with app programming and I am trying to program my first test app. 因此,我从应用程序编程开始,并且尝试对我的第一个测试应用程序进行编程。 The main pupose of the app is that it acts like a dice and gives a random number between 1 and 6. However when I push the roll button the app crashes 该应用程序的主要用途是它像骰子一样工作,并提供1到6之间的一个随机数。但是,当我按“滚动”按钮时,该应用程序会崩溃

My mainActivity code is as follows: 我的mainActivity代码如下:

package com.example.second;

import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@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;
}
public void roll(View v){
    int min = 1;
    int max = 7;

    Random r = new Random();
    int i1 = r.nextInt(max - min + 1) + min;
    EditText random = (EditText)findViewById(R.id.randomtxt);
    random.setText(i1);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
 }
}

And the .xml code is .xml代码是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.second.MainActivity" >

<Button
    android:id="@+id/roll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="106dp"
    android:layout_marginTop="124dp"
    android:onClick="roll"
    android:text="@string/roll" />

<TextView
    android:id="@+id/randomtxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/roll"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/roll"
    android:layout_marginTop="48dp"
    android:text="@string/num1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

   </RelativeLayout>

The error that I get is Emulator] setWindowSurfaceColorBuffer: bad color buffer handle 0 , but I do not think this has anything to do with the crashing off the app. 我得到的错误是[ Emulator] setWindowSurfaceColorBuffer: bad color buffer handle 0 ,但我认为这与崩溃应用程序无关。

And because I cannot find any problems (nor does SDK provide any) I have no idea why my app crashes. 而且由于找不到任何问题(SDK也未提供任何问题),我也不知道为什么我的应用程序崩溃。

You're trying to cast a TextView to an EditText . 您正在尝试将TextView转换为EditText Either change your XML layout to define randomtxt as an <EditText> , or what you probably want is to just change this line: 更改XML布局以将randomtxt定义为<EditText> ,或者您可能只想更改此行:

EditText random = (EditText)findViewById(R.id.randomtxt);

to this: 对此:

TextView random = (TextView)findViewById(R.id.randomtxt);

You also probably want to change your setText line to: 您可能还想将setText行更改为:

random.setText(Integer.toString(i1));

Although there are many more options to cast you integer value to a String. 尽管还有更多选项可以将整数值转换为字符串。

cast error textview to edittext and you cannot settext(integervalue) -> settext(String.valueof(integer)) 将错误的textview强制转换为edittext并且您不能settext(integervalue)-> settext(String.valueof(integer))

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         int min = 1;
            int max = 6;

            Random r = new Random();
            int i1 = r.nextInt(max - min + 1) + min;

            random.setText(String.valueOf(i1));

    }
});

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

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