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
My mainActivity
code is as follows:
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
<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.
And because I cannot find any problems (nor does SDK provide any) I have no idea why my app crashes.
You're trying to cast a TextView
to an EditText
. Either change your XML layout to define randomtxt
as an <EditText>
, or what you probably want is to just change this line:
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:
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))
@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));
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.