No errors on android studio but crashes when trying to show random sentence on Text View. So I am a beginner and trying to do app that randomizes sentence, this crashes on every emulator and phone I've tried it on, what could be the problem? Thanks for answers in advance!
Game.java :
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Game extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final TextView textView2 = (TextView) findViewById(R.id.textView2);
Button button2 = (Button) findViewById(R.id.button2);
final String[] lauseet = {"Moro moro", "Tere tere", "Heippa hei", "Nakkimuki ja vesi"};
final int rando = (int) (Math.random()*4);
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
textView2.setText(lauseet[rando]);
}
});
}
}
And
The xml file for this:
<?xml version="1.0" encoding="utf-8"?>
<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="fi.itsn.junttipeli.Game">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Tämä on väitekenttä"
android:id="@+id/textView2"
android:onClick="klikk"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="@color/abc_primary_text_disable_only_material_light" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Seuraava"
android:id="@+id/button2"
android:textStyle="bold"
android:onClick="klikk"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
答案 0 :(得分:1)
I really don't see any real (read error causing) problem with the code but, I think based on your intent, you are looking for something like:
final TextView textView2 = (TextView) findViewById(R.id.textView2);
Button button2 = (Button) findViewById(R.id.button2);
final String[] lauseet = {"Moro moro", "Tere tere", "Heippa hei", "Nakkimuki ja vesi"};
final Random random = new Random();
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
textView2.setText(lauseet[random.nextInt(4)]);
}
});
答案 1 :(得分:0)
Your String array have only 4 items in it , when you create random number your multiplying double value with the 4 which may return some value like 4.1253527 when you set that random value to index of the string which may cause out of bounds exception when you click on the button solution is
Random ran= new Random();
int rando = ran.nextInt(4);// here it will return the random number between 1 to 4
答案 2 :(得分:0)
显示的代码相当薄弱,应该更像是:
static final String[] lauseet = {"Moro moro", "Tere tere", "Heippa hei", "Nakkimuki ja vesi"};
Random random = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
final TextView textView2 = (TextView) findViewById(R.id.textView2);
textView2.setText(lauseet[random.nextInt(lauseet.length)]);
}
});
}
虽然lauseet值实际上应该是xml中的TypedArray。
但重点是它没有崩溃就可以正常工作。
没有崩溃。发布崩溃的logcat和实际崩溃的文件,很可能是在xml中,或者你的命名空间是错误的。
https://developer.android.com/studio/debug/index.html 请参阅:查看系统日志
PS。评论辩论: 将double转换为int,会导致int被覆盖。所以[0,1]变为[0-4]总是向下舍入。范围从0.0到1.0(仅限1.0)(您将不会获得1.0)。因此,当它被覆盖时,它将始终给出0,1,2,3并且这些数字将被正确且均匀地分布。