简体   繁体   中英

What is a better way to structure my code here?

I'm a newbie trying to write an app which input/outputs a number between 1-10 after pressing a button. I'm hoping to have this code throw an exception when the input value is outside the 1-10 boundary.

Caused by: java.lang.reflect.InvocationTargetException

I believe it has to do with the way I put my rand() function inside the onClick() listener. Am I on the right track that it's just written very poorly?

Thanks very much if you can help.

Here is my code:

 public Button button; public TextView textView; public EditText editText; Random r; public int max=0; public int min=0; public int temp=0; @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); }

Also, here is my XML

 <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" />

U have to put your function call into a try/catch block:

public void onClick(View view) {

try{
  rand(Integer.parseInt(editText.getText().toString()));
}catch(IndexOutOfBoundsException e){
  e.printStackTrace();
}

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.

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