Search code examples
androidandroid-studiotouchtouch-event

Attempt to invoke virtual method 'int android.view.MotionEvent.getAction()' on a null object reference


i'm trying to catch any Touch event from the user to use it on a boolean verification (like a "press on the screen to continue"). but i getting an error which is the question title. the line which says:

resultadoPalavra.setText("Você acertou! Pressione na tela para continuar");

is that "press on the screen continue" i had mentioned.

the code:

public class ApurarResultadoFragment extends Fragment{

private TextView resultadoPalavra;
String palavraDigitada;
String resposta;
MainActivity mActivity = new MainActivity();
private boolean isTouch = false;
MotionEvent event;


public ApurarResultadoFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_apurar_resultado, container, false);
    resultadoPalavra = (TextView) view.findViewById(R.id.TextViewTesteFragment);
    palavraDigitada = this.getArguments().getString("oqSeraFalado");
    resposta = this.getArguments().getString("resposta");

    if(testeResultado()){
        resultadoPalavra.setText("Você acertou! Pressione na tela para continuar");
        if(onTouchEvent(view, event)){
            Log.v("teste","" + isTouch);
        }

    }
    return view;
}

public boolean onTouchEvent(View v, MotionEvent event){
    int eventaction = event.getAction();
    switch (eventaction){
        case MotionEvent.ACTION_DOWN:
            isTouch = true;
            break;
        case MotionEvent.ACTION_UP:
            isTouch = true;
            break;
        case MotionEvent.ACTION_MOVE:
            isTouch = true;
            break;
    }
    return true;
}

public boolean testeResultado() {
    if (resposta.equalsIgnoreCase(palavraDigitada)){
        return true;
    }else{
        return false;
    }
}
}

How can i initialize the variable "event" ? i guess her is the problem


Solution

  • Here's how to add TouchListener to the view:

    view.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            int eventaction = event.getAction();
            switch (eventaction) {
                case MotionEvent.ACTION_DOWN:
                    isTouch = true;
                    break;
                case MotionEvent.ACTION_UP:
                    isTouch = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    isTouch = true;
                    break;
            }
            return true;
            }
        });
    

    But in your case, I'd use OnClickListener instead:

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("teste", "touched");
        }
    });