Error calling non Static function from a static context

Asked

Viewed 54 times

1

I created a class only to call 2 methods that cannot be static. But when calling the method of this object returns the error that I am not able to interpret.

Code:

public static Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        Bundle bundle = msg.getData();
        byte[] data = bundle.getByteArray("data");
        String dataString = new String(data);
        //resultTEXT.setText(dataString);
        if (dataString.equals("---N"))
            resultTEXT.setText("Ocorreu um erro durante a conexão D:");
        if (dataString.equals("---S"))
            resultTEXT.setText("Conectado :D");
        if (dataString.equals("---O")) {
            Exe e = new Exe();
            e.executarAcoes();
            //se a mensagem recebida for a configurada no arduino é porque o botao foi clicado

        }

    }

};

Class I call the method prompSpeechInput which cannot be static:

class Exe extends MainActivity { 
    public void executarAcoes(){
        promptSpeechInput();
    }

}

Error log:

    D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.marti.appfeirav2, PID: 23362
                  java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
                      at android.app.Activity.startActivityForResult(Activity.java:3796)
                      at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
                      at android.app.Activity.startActivityForResult(Activity.java:3744)
                      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
                      at com.example.marti.appfeirav2.MainActivity.promptSpeechInput(MainActivity.java:110)
                      at com.example.marti.appfeirav2.Exe.executarAcoes(MainActivity.java:221)
                      at com.example.marti.appfeirav2.MainActivity$3.handleMessage(MainActivity.java:158)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5608)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)
I/art: Background partial concurrent mark sweep GC freed 2437(382KB) AllocSpace objects, 0(0B) LOS objects, 27% free, 5MB/7MB, paused 726us total 103.098ms

/ Activity that must be called. For me to have access to it, I played inside an instantiated object, because my Handler is Static, and I can’t access it directly because it is non-static

public void promptSpeechInput(){
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Diga algo");
    try {
        startActivityForResult(i, 100);
    }catch(ActivityNotFoundException ex){
        Toast.makeText(MainActivity.this, "Dispositivo nao suporta", Toast.LENGTH_LONG).show();

    }

}

Here is the code of the class where I call the method, remembering that prompspeechinput is from mainActivity

class Exe extends MainActivity {
    public void executarAcoes(){
        promptSpeechInput();
    }

}
  • 1

    Surely your Bundle is going as null, see in your log: java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference

  • Could post code of activities(s)?

  • Yes, I’ll edit the question.

  • @Wotonsampaio But this activity of mine works normally if I call for the action of a button on the screen, for example. This my Handler that "gets" the click button on the Arduin. I reviewed it and for me it was normal, I will post the code, if you can look. I thank you.

  • 1

    Exe is an Activity so you can not do Exe e = new Exe(); e.executarAcoes();

  • so as I can call a method of Activity, I tried to isolate in a class, but it did not work. As it uses the main context

Show 1 more comment
No answers

Browser other questions tagged

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