I`ve made an app with following code
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView t1, t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
t2 = (TextView) findViewById(R.id.textView4);
t1 = (TextView) findViewById(R.id.textView3);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = editText.getText().toString();
int user = Integer.parseInt(number);
int computer = random();
int lose = random();
int dif1 = wartoscbez(lose-user);
int dif2 = wartoscbez(lose-computer);
if(dif1>dif2)
{
t1.setText(computer);
t2.setText("YOU WON");
}
if (dif2>dif1)
{
t1.setText(computer);
t2.setText("YOU LOSE");
}
if(dif1==dif2)
{
t1.setText(computer);
t2.setText("YOU WON");
}
}
});
}
public int random()
{
int max = 1;
int min = 50;
Random r = new Random();
int aNumber = r.nextInt(max - min + 1) + min;
return aNumber;
}
public int wartoscbez(int a)
{
if (a>=0)
{
return a;
}
else
{
int x = 0 - a;
return x;
}
}
}
app compiles, but in emulator apps closes unfortunately. In logCat I have something like that:
10-04 17:36:56.946 3717-3717/? I/art: Not late-enabling -Xcheck:jni (already on) 10-04 17:36:56.947 3717-3717/? W/art: Unexpected CPU variant for X86 using defaults: x86 10-04 17:36:57.168 3717-3717/com.holland.porazpierwszy W/System: ClassLoader referenced unknown path: /data/app/com.holland.porazpierwszy-1/lib/x86 10-04 17:36:57.190 3717-3717/com.holland.porazpierwszy I/InstantRun: starting instant run server: is main process 10-04 17:36:57.328 3717-3717/com.holland.porazpierwszy W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 10-04 17:36:57.625 3717-3749/com.holland.porazpierwszy I/OpenGLRenderer: Initialized EGL, version 1.4 10-04 17:36:57.627 3717-3749/com.holland.porazpierwszy D/OpenGLRenderer: Swap behavior 1 10-04 17:36:57.627 3717-3749/com.holland.porazpierwszy W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 10-04 17:36:57.628 3717-3749/com.holland.porazpierwszy D/OpenGLRenderer: Swap behavior 0 10-04 17:36:57.751 3717-3717/com.holland.porazpierwszy W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 10-04 17:37:16.351 3717-3717/com.holland.porazpierwszy D/AndroidRuntime: Shutting down VM 10-04 17:37:16.352 3717-3717/com.holland.porazpierwszy E/AndroidRuntime: FATAL EXCEPTION: main Process: com.holland.porazpierwszy, PID: 3717 java.lang.IllegalArgumentException: n must be positive at java.util.Random.nextInt(Random.java:391) at com.holland.porazpierwszy.MainActivity.random(MainActivity.java:60) at com.holland.porazpierwszy.MainActivity$1.onClick(MainActivity.java:30) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Do you know how to fix it?
Aucun commentaire:
Enregistrer un commentaire