Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- com.example.android.javapractice E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.example.android.javapractice, PID: 13735
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.javapractice/com.example.android.javapractice.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
- <TextView
- android:id="@+id/test"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/turn_on"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="button"/>
- package com.example.android.javapractice;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity {
- TextView test = findViewById(R.id.test);
- Button turn = findViewById(R.id.turn_on);
- int count = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- turn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- keepRoll();
- }
- });
- }
- public int rollDice(){
- int num = 0;
- int roll;
- Random randomRoll = new Random();
- roll = randomRoll.nextInt(6)+1;
- System.out.println("Roll is: "+roll);
- num += roll;
- return num;
- }
- public void keepRoll() {
- int dice1 = rollDice();
- int dice2 = rollDice();
- int dice3 = rollDice();
- while (!(dice1 == dice2 && dice2 == dice3)){
- dice1 = rollDice();
- dice2 = rollDice();
- dice3 = rollDice();
- count += 1;
- }
- test.setText(count);
- }
- }
Add Comment
Please, Sign In to add comment