输入输出编号在1-10之间

时间:2019-02-28 15:33:34

标签: java android

我是新手,试图编写输入和输出1-10之间的数字的代码。如果输入大于10,我希望用户输入其他数字。每当我尝试运行它时,它就会崩溃。 我还尝试将rand()函数代码直接嵌入到onClick()侦听器中,但无济于事。谢谢你的帮助。

以下是错误:

 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.Random.nextInt(int)' on a null object reference

这是我所拥有的:

package com.abc.****

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    public Button button;
    public TextView textView;
    public EditText editText;
    Random r;
    public int max, min;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button)findViewById(R.id.button);
        editText=(EditText)findViewById(R.id.editText);
        editText.setText("");
        textView=(TextView) findViewById(R.id.output);

    }

    public void onClick(View view) {

        rand(Integer.parseInt(editText.getText().toString()));


        }


    public void rand(int temp) throws IndexOutOfBoundsException{


        temp = Integer.parseInt(editText.getText().toString());
        if(temp >10 || temp<0){
            throw new IndexOutOfBoundsException("out of bounds... between 1-10");
        }
        if(!editText.equals("")){
            min = 10-temp;
            max = r.nextInt(min + 1)+1;

        }
        String set = String.valueOf(max);

        textView.setText(set);

    }

    public void message(View view){
        Snackbar snackbar = Snackbar.make(findViewById(R.id.rootLayout),"Number between 1 - 10", Snackbar.LENGTH_SHORT);
        snackbar.show();

    }




    }

也是我的XML

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="3"
    tools:context=".MainActivity"
    tools:ignore="LabelFor"
    android:id="@+id/rootLayout"
    android:padding="16dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    </android.support.v7.widget.Toolbar>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="0"
        android:text="Enter a Number Between 1 and 10:"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="2"
        android:layout_column="0"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/button"
        android:layout_width="369dp"
        android:layout_height="wrap_content"
        android:layout_row="3"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:text="Button"
        android:onClick="onClick"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="4"
        android:layout_column="0"
        android:text="output"
        android:textSize="30sp" />


</GridLayout>

0 个答案:

没有答案