Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android-BroadcastReceiver-java.lang.StackOverflower错误_Java_Android_Broadcastreceiver_Stack Overflow - Fatal编程技术网

android-BroadcastReceiver-java.lang.StackOverflower错误

android-BroadcastReceiver-java.lang.StackOverflower错误,java,android,broadcastreceiver,stack-overflow,Java,Android,Broadcastreceiver,Stack Overflow,尝试接收广播时,我收到StackOverflower错误 我假设这个错误是由于广播发送的次数造成的,但是当我试图获取我正在广播的活动的上下文时,它会抛出错误。我也会把我的代码贴在下面 D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB at com.example.curie.fairbanks_01.InputActivity.getContext(InputActiv

尝试接收广播时,我收到StackOverflower错误 我假设这个错误是由于广播发送的次数造成的,但是当我试图获取我正在广播的活动的上下文时,它会抛出错误。我也会把我的代码贴在下面

        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();
            }
        }



    }

广播接收器应该这样设置

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

您有一个方法调用它自己。你以为会发生什么<代码>公共静态上下文getContext(){return getContext();}getContext调用自身…递归方法调用。。。getContext一直运行,直到出现错误为止。
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