I created a class only to call 2 methods that can not be static. However calling the method of this object returns the error that I can not 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 that I call the prompSpeechInput
method that can not 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 to call. For me to have access to it, I've played inside an instantiated object, since my handler is static, and I can not 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 class code where I call the method, remembering that prompspeechinput is from mainActivity
class Exe extends MainActivity {
public void executarAcoes(){
promptSpeechInput();
}
}