Problems with the instantiation of the chronometer

Asked

Viewed 68 times

0

Good afternoon guys, in my application I am creating a "simple" chronometer but I’m having trouble running application. I created a class that contains two methods one of start and the other of pause even then it is right, in my Java App, I instated the class and called the method in the onclick of a boot and the other onclick called pause.... by executing my Activity he presents me with this error...

java.lang.Nullpointerexception: Attempt to invoke virtual method 'void android.widget.Chronometer.setBase(long)' on a null Object Reference

From what I’ve been looking on the internet it seems that my method needs to be the long type... but I don’t know how to install a long class, I looked on the internet too and did not know how to solve, Could someone help me? follows the code below:

chronometer class:

package com.allsport.miyonic.allsport.Controller;

import android.os.SystemClock;
import android.view.View;
import android.widget.Chronometer;


/**
 * Created by nathan on 22/06/2017.
 */

public class TempoJogo {

    private View.OnClickListener context;
    private Chronometer reloginho;
    private long milliseconds;

    /*===========Construct==========*/
    public TempoJogo(View.OnClickListener context, Chronometer reloginho, long milliseconds){
        this.context = context;
        this.reloginho = reloginho;
        this.milliseconds = milliseconds;
    }

    public void startJogo(){
        /*definindo o milliseconds em zero para poder armazenar o valor que for pausado do systema */

        milliseconds = 0;
        reloginho.setBase(SystemClock.elapsedRealtime() - milliseconds);
        reloginho.start();
    }

    public void pausarJogo(){
        /* pegar último valor armazenado no milliseconds menos  o valor atual do systema (0)
         retornando o valor atual da activity*/

        milliseconds = SystemClock.elapsedRealtime() - reloginho.getBase();
        reloginho.stop();
    }
}

class Activity (Simpleshome):

package com.allsport.miyonic.allsport.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import com.allsport.miyonic.allsport.Controller.TempoJogo;
import com.allsport.miyonic.allsport.Dialog.DialogFaltaAmarela;
import com.allsport.miyonic.allsport.Dialog.DialogFaltaVermelho;
import com.allsport.miyonic.allsport.Dialog.DialogGol;
import com.allsport.miyonic.allsport.Dialog.DialogInformaction;
import com.allsport.miyonic.allsport.Dialog.DialogSair;
import com.allsport.miyonic.allsport.R;
import com.allsport.miyonic.allsport.Controller.TempoTese;

import Base.*;

public class SimplesHome extends AppCompatActivity {

    private ImageButton imgButton_play, imgButton_pause, golCasa, golFora, vermlho, amarelo;
    private TempoJogo tempo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_simples);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        golCasa = (ImageButton) findViewById(R.id.golCasaId);
        golFora = (ImageButton) findViewById(R.id.golForaId);
        imgButton_play = (ImageButton) findViewById(R.id.imgButton_play);
        imgButton_pause = (ImageButton) findViewById(R.id.imgButton_pause);


        golCasa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador++;
                valorOne.setText("" + contador);

                Context alerta = getApplicationContext();
                CharSequence text = "Goooll!";
                int duracao = Toast.LENGTH_SHORT;
                Toast lert = Toast.makeText(alerta, text, duracao);
                lert.show();

                DialogGol golzinho = new DialogGol();
                golzinho.show(getSupportFragmentManager(), "golzinho_tag");
            }
        });

        golFora.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                contador1++;
                valorDouble.setText("" + contador1);

                Context gol = getApplication();
                CharSequence texto = "Goooll!";
                int tempo = Toast.LENGTH_SHORT;
                Toast apresentar = Toast.makeText(gol, texto, tempo);
                apresentar.show();

                DialogGol golzinhoTwo = new DialogGol();
                golzinhoTwo.show(getSupportFragmentManager(), "golzinhoTow_tag");
            }
        });

        imgButton_play.setEnabled(true);
        imgButton_pause.setEnabled(false);
        golCasa.setEnabled(false);
        golFora.setEnabled(false);

        imgButton_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(false);
                imgButton_pause.setEnabled(true);
                golCasa.setEnabled(true);
                golFora.setEnabled(true);

                tempo.startJogo();
            }
        });

        imgButton_pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgButton_play.setEnabled(true);
                imgButton_pause.setEnabled(false);
                golCasa.setEnabled(false);
                golFora.setEnabled(false);

                tempo.pausarJogo();
            }
        });

    }
}

Thank you....

  • On which line of code is the error?

  • In this line: reloginho.setBase(SystemClock.elapsedRealtime() - milliseconds);

  • @Nathan, you don’t seem to have introduced the clock variable, by the code.

  • I made a change to the code

1 answer

0

You don’t seem to have initialized this "clock" object anywhere in the code, so when you try to call a method in something null, it gives the error that you posted.

  • So the clock is a widget that I called the class Simpleshome (extends Simpleshome), I cast it (getting the id) and I’m using in my class Tempojogo

  • Okay, but the "Tempojogo" class has no way of knowing that.

  • Ahhh... How would I do that? how to make the Tempojogo class know that the clock is a Chronometer widget?

  • Ué, initializes the clock in the Tempojogo constructor (you don’t seem to have defined a constructor). I also don’t understand why you extended this Simplehome class which is an Activity.

  • The idea was... as I can not set the cast of the widget in the Tempojogo class, I would take the Simpleshome class and for this I would make an exteds of this Activity class. Doing it this way is wrong?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.