android- BroadcastReciever -java.lang.StackOverflowError

时间:2018-05-29 14:26:24

标签: java android broadcastreceiver stack-overflow

尝试接收广播时,我正在发现StackOverflowError 我假设这个错误是由于广播发送的次数而产生的,但是当我尝试获取我正在广播的活动的上下文时它会抛出错误。我也会发布我的代码。

感谢您的帮助!

        D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairbanks_01.InputActivity.getContext(InputActivity.java:56)
                at com.example.curie.fairba
                ERR: TOTAL BYTES WRITTEN: 1447748

这是进行连接并广播它的程序。

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.Scanner;
import java.util.UUID;
// import org.apache.commons.io.IOUtils;

    public class BluetoothConnection extends Thread {

        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        byte[] buffer;
        BluetoothAdapter mmAdapter;

        public static String output = "ABC";


        // Unique UUID for this application, you may use different
        private static  UUID MY_UUID ;



        public BluetoothConnection(BluetoothDevice device) {

            UUID MY_UUID = device.getUuids()[0].getUuid();

            BluetoothSocket tmp = null;
            mmAdapter = null;
            System.out.println("$$$$$$$$$$$$$$$$****** in constructor ******$$$$$$$$$$$$$$$$");

            // Get a BluetoothSocket for a connection with the given BluetoothDevice
            try {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mmSocket = tmp;

            //now make the socket connection in separate thread to avoid FC
            Thread connectionThread  = new Thread(new Runnable() {

                @Override
                public void run() {
                    // Always cancel discovery because it will slow down a connection
                    Log.d("workkkkkk","$$$$$$$$$$$$$$$$****** printingggggg ******$$$$$$$$$$$$$$$$");

                    //mmAdapter.cancelDiscovery();

                    // Make a connection to the BluetoothSocket
//                    try {
//                        // This is a blocking call and will only return on a
//                        // successful connection or an exception
//                        mmSocket.connect();
//
//
//                        System.out.println("\n\n\n\n\n\n socket connected\n\n\n\n\n\n");
//
//                    } catch (IOException e) {
//                        //connection to device failed so close the socket
//                        try {
//                            mmSocket.close();
//                            System.out.println("\n\n\n\n\n\n socket closed\n\n\n\n\n\n");
//
//                        } catch (IOException e2) {
//                            System.out.println("\n\n\n\n\n\n in catch\n\n\n\n\n\n");
//
//                            e2.printStackTrace();
//                        }
//                    }
                }
            });

            connectionThread.start();

            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                try {
                    // This is a blocking call and will only return on a
                    // successful connection or an exception
                    mmSocket.connect();


                    System.out.println("$$$$$$$$$$$$$$$$****** socket connected ******$$$$$$$$$$$$$$$$");

                } catch (IOException e) {
                    //connection to device failed so close the socket
                    try {
                        mmSocket.close();
                        System.out.println("$$$$$$$$$$$$$$$$****** socket closed ******$$$$$$$$$$$$$$$$");

                    } catch (IOException e2) {
                        System.out.println("$$$$$$$$$$$$$$$$****** in catch ******$$$$$$$$$$$$$$$$");

                        e2.printStackTrace();
                    }
                }

                tmpIn = mmSocket.getInputStream();
                tmpOut = mmSocket.getOutputStream();
                buffer = new byte[50];
            } catch (IOException e) {
                e.printStackTrace();
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }


        public void run() {

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    //read the data from socket stream
                    if(mmInStream != null) {
                      mmInStream.read(buffer);
                      for(byte b : buffer)
                      {
                          char c = (char) b;
                          if(c >=' ' && c <'z') {
                            System.out.print(c);
                            output += c;
                          }

                      }
                       System.out.println();
                        Intent intent = new Intent("com.example.curie.fairbanks_01");
                        intent.putExtra("output",output);
                        LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcast(intent);
                        output = "";
                    }
                    // Send the obtained bytes to the UI Activity
                } catch (IOException e) {
                    //an exception here marks connection loss
                    //send message to UI Activity
                    break;
                }
            }
        }

        public void write(byte[] buffer) {
            try {
                //write the data to socket stream
                if(mmOutStream != null)
                mmOutStream.write(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }

此程序接收广播并抛出错误。

import android.bluetooth.BluetoothDevice;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.support.v4.content.LocalBroadcastManager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class InputActivity extends AppCompatActivity {

         TextView inputWindow;
         Button readWeight;
         BroadcastReceiver resultReceiver;

        BluetoothDevice instrument;
        BluetoothConnection connector;
        String weight;

        @Override
        protected void onCreate(Bundle savedInstanceState) {


            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_input);

            inputWindow = (TextView) findViewById(R.id.input_window);
            readWeight = (Button) findViewById(R.id.read_weight);

            instrument = DeviceList.instrument;
            System.out.println("$$$$$$$$$$$$$$$$****** In new Activity ******$$$$$$$$$$$$$$$$");
            connector = new BluetoothConnection(instrument);

            inputWindow.setText(BluetoothConnection.output);

            readWeight.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    connector.run();
                    //readWeight.setVisibility(View.INVISIBLE);
                }
            });

            resultReceiver = createBroadcastReceiver();
            LocalBroadcastManager.getInstance(this).registerReceiver(resultReceiver, new IntentFilter("com.intertech.random.generation"));

        }


        public static Context getContext()
        {
            return getContext();

        }

        private BroadcastReceiver createBroadcastReceiver() {
            return new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    updateResults(intent.getStringExtra("result"));
                }
            };
        }

        public void updateResults(String update)
        {
            inputWindow.setText(update);

        }




    }

1 个答案:

答案 0 :(得分:1)

广播接收器应这样设置...

private BroadcastReceiver createBroadcastReceiver() {
    return new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            weight = intent.getStringExtra("Output");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //what you want to happen
                }
            });
        }
    };
}// end createBroadcastReceiver