Error executing Startactivityforresult() line

Asked

Viewed 171 times

0

Could anyone interpret why that mistake? The idea is that every Bluetooth algorithm is in a separate class, and with the main one I only call the methods. Follow the error and the code.

package com.example.audio_auto;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class AudioActivity extends Activity{

    private final static int VOICE_RESULT = 1;
    private ImageButton btnSpeack;
    private Button btSair, connect, enviar;
    private TextView txtText, txt;
    volatile boolean stopWorker;
    byte[] readBuffer;
    int readBufferPosition;
    int counter;
    Blue BluClass = new Blue(); 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio);

        enviar = (Button) findViewById(R.id.button2);
        connect = (Button) findViewById(R.id.button3);
        btSair = (Button) findViewById(R.id.button4);
        btnSpeack = (ImageButton) findViewById(R.id.imageButton1);
        txtText = (TextView) findViewById(R.id.textView1);
        txt = (TextView) findViewById(R.id.textView2);

        btnSpeack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(
                        RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                        "Favor fala no microfone");

                try {
                    startActivityForResult(intent, VOICE_RESULT);
                    txtText.setTextColor(Color.BLUE);
                    txtText.setText("");
                    // verifica();

                } catch (ActivityNotFoundException e) {
                    Toast t = Toast.makeText(getApplicationContext(),
                            "nao suporta seu dispositivo", Toast.LENGTH_LONG);
                    t.show();
                }
            }
        });

        btSair.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BluClass.conect();
            }
        });

        enviar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == VOICE_RESULT && resultCode == RESULT_OK) {
            ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            txtText.setText(matches.get(0).toString());
        }

        super.onActivityResult(requestCode, resultCode, data);

        String[] confirmar = { "bola", "dado" };

        for (int i = 0; i < confirmar.length; i++) {

            if (txtText.getText().toString().contains(confirmar[i])) {

                txt.setTextColor(Color.GREEN);
                txt.setText("Otimo");
                break;
            } else {
                txt.setTextColor(Color.RED);
                txt.setText("Comando Desconhecido !");
            }
        }
    }
}

Class where I will put all the Bluetooth algorithm

package com.example.audio_auto;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;

public class Blue extends Activity{
    BluetoothDevice mDevice;

    public  void conect(){
        BluetoothAdapter BT = BluetoothAdapter.getDefaultAdapter ();
        if (!BT.isEnabled()) {
            Intent enableBtIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            int REQUEST_ENABLE_BT = 0;
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.audio_auto"
    android:versionCode="1"
    android:versionName="1.1" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.audio_auto.AudioActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Log:

11-12 23:07:35.393: E/MtpService(29595): In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
11-12 23:07:35.393: E/MtpService(29595): battPlugged Type : 2
11-12 23:07:36.114: E/AndroidRuntime(30316): FATAL EXCEPTION: main
11-12 23:07:36.114: E/AndroidRuntime(30316): java.lang.NullPointerException
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.app.Activity.startActivityForResult(Activity.java:3390)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.app.Activity.startActivityForResult(Activity.java:3351)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at com.example.audio_auto.Blue.conect(Blue.java:18)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at com.example.audio_auto.AudioActivity$3.onClick(AudioActivity.java:82)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.view.View.performClick(View.java:4162)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.view.View$PerformClick.run(View.java:17088)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.os.Handler.handleCallback(Handler.java:615)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.os.Looper.loop(Looper.java:137)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at android.app.ActivityThread.main(ActivityThread.java:4867)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at java.lang.reflect.Method.invokeNative(Native Method)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at java.lang.reflect.Method.invoke(Method.java:511)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
11-12 23:07:36.114: E/AndroidRuntime(30316):    at dalvik.system.NativeStart.main(Native Method)
11-12 23:07:36.184: E/android.os.Debug(2027): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

1 answer

1

You can’t inherit from Activity only to use the method startActivity and correlates.

You can only inherit from Activity solely and exclusively to create Activity's, because most of the methods of this class are dependent on change of states caused by the life cycle and would generate error because of the absence of state, which was their case.

Instead of inheriting, it is enough to require the presence of a Context as parameter. Ai just use this Context for the calls it would make to Activity. If necessary you can swap the class of the parameter for Activity, but I believe that the Context is enough. This pattern is very common, you can see in other projects, it is no problem.

Change your class Blue for:

public class Blue extends Activity{
    BluetoothDevice mDevice;

    public  void conect(Context context){
        BluetoothAdapter BT = BluetoothAdapter.getDefaultAdapter();
        if (! BT.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            int REQUEST_ENABLE_BT = 0;
            context.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }
    }
}

And change the method call connect for BluClass.conect(AudioActivity.this)

  • Dear thank you , do not test yet because I’m on the job but thanks and who arranged the blue class on the site was worth tmbém because I was not able to format here on the right site (it was all embolado...)Thanks wakim I will research more about this

Browser other questions tagged

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