应用程序意外停止强制关闭?

时间:2015-05-10 22:54:29

标签: android

当我运行我的应用程序时,它已经意外停止并在我添加Edittext时给出了强制关闭错误。

我的应用程序是为两部手机之间的语音聊天而创建的

如果您需要更多细节请问我。

这里我展示了我的代码:

MainActivity:

public class MainActivity extends Activity {

private EditText target,port;
private Button startButton,stopButton;
public byte[] buffer;
public static DatagramSocket socket;
private int port_num=1370;         //which port??
AudioRecord recorder;

//Audio Configuration. 
private int sampleRate = 256;      //How much will be ideal?
private int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;    
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;       

private boolean status = true;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(getApplicationContext(), "App created...!",Toast.LENGTH_LONG).show();
    //target = (EditText) findViewById (R.id.target_IP);

    startButton = (Button) findViewById (R.id.start_button);
    stopButton = (Button) findViewById (R.id.stop_button);
    //port=(EditText) findViewById(R.id.editText1);


    startButton.setOnClickListener (startListener);
    stopButton.setOnClickListener (stopListener);
}

private final OnClickListener stopListener = new OnClickListener() {
    @Override
    public void onClick(View arg0) {

        Toast.makeText(getApplicationContext(), "Recoder stop..",Toast.LENGTH_LONG).show();
        status = false;
        recorder.release();
        Log.d("VS","Recorder released");
    }

};

private final OnClickListener startListener = new OnClickListener() {

      @Override
        public void onClick(View arg0) {
          Toast.makeText(getApplicationContext(), "Clicked....!",Toast.LENGTH_LONG).show();
                    status = true;
                    startStreaming();           
        }
};


public void startStreaming() {


    Thread streamThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                //Integer.parseInt(port.getText().toString())
                port_num=1370;
                int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);
                DatagramSocket socket = new DatagramSocket();
                Log.d("VS", "Socket Created");

                byte[] buffer = new byte[minBufSize];

                Log.d("VS","Buffer created of size " + minBufSize);
                DatagramPacket packet;

                final InetAddress destination = InetAddress.getByName("192.168.43.80");
                Log.d("VS", "Address retrieved");


                recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,sampleRate,channelConfig,audioFormat,minBufSize);
                Log.d("VS", "Recorder initialized");

                recorder.startRecording();

                Toast.makeText(getApplicationContext(), "Start Talking....",Toast.LENGTH_LONG).show();;
                while(status == true) {


                    //reading data from MIC into buffer
                    minBufSize = recorder.read(buffer, 0, buffer.length);

                    //putting buffer in the packet
                    packet = new DatagramPacket (buffer,buffer.length,destination,1370);

                    socket.send(packet);
                }
            } catch(UnknownHostException e) {
                Log.e("VS", "UnknownHostException");
            } catch (IOException e) {
                Log.e("VS", "IOException");
            } 
        }

    });
    streamThread.start();
 }
}

请给我一个正确的指导来解决我的错误。提前谢谢!

log cat:

05-11 08:59:26.152: W/Trace(892): Unexpected value from      nativeGetEnabledTags: 0
05-11 08:59:26.483: W/Trace(892): Unexpected value from   nativeGetEnabledTags: 0
05-11 08:59:26.483: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:27.712: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:27.782: I/Choreographer(892): Skipped 134 frames!  The application may be doing too much work on its main thread.
05-11 08:59:27.782: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0

05-11 08:59:29.044: I/Choreographer(892): Skipped 211 frames!  The application may be doing too much work on its main thread.

05-11 08:59:29.472: D/gralloc_goldfish(892): Emulator without GPU emulation detected.

05-11 08:59:46.802: I/Choreographer(892): Skipped 66 frames!  The application may be doing too much work on its main thread.
05-11 08:59:46.802: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:46.813: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:46.842: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:46.842: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.332: I/Choreographer(892): Skipped 65 frames!  The application may be doing too much work on its main thread.
05-11 08:59:47.332: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.332: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.392: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.392: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.802: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.802: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.835: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:47.835: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.392: I/Choreographer(892): Skipped 73 frames!  The application may be doing too much work on its main thread.
05-11 08:59:48.392: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.413: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.442: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.442: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.862: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.862: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.922: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:48.952: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:49.382: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:49.382: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:49.432: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:49.462: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.014: I/Choreographer(892): Skipped 68 frames!  The application may be doing too much work on its main thread.
05-11 08:59:50.014: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.022: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.059: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.059: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.614: I/Choreographer(892): Skipped 64 frames!  The application may be doing too much work on its main thread.
05-11 08:59:50.614: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.622: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.685: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:50.732: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.062: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.062: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.292: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.292: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.593: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.593: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.744: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:51.744: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:52.172: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:52.172: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:52.282: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:52.292: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:52.732: I/Choreographer(892): Skipped 137 frames!  The application may be doing too much work on its main thread.
05-11 08:59:52.732: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:52.742: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.012: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.012: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.132: I/Choreographer(892): Skipped 48 frames!  The application may be doing too much work on its main thread.
05-11 08:59:53.132: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.162: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.272: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.272: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.742: I/Choreographer(892): Skipped 65 frames!  The application may be doing too much work on its main thread.
05-11 08:59:53.742: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.742: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.822: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:53.822: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.252: I/Choreographer(892): Skipped 65 frames!  The application may be doing too much work on its main thread.
05-11 08:59:54.252: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.252: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.293: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.332: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.752: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.752: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.782: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:54.792: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.282: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.282: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.312: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.342: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.832: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.832: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.852: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:55.872: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:56.363: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:56.363: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:56.382: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:56.392: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:56.963: I/Choreographer(892): Skipped 65 frames!  The application may be doing too much work on its main thread.
05-11 08:59:56.963: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:56.963: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.005: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.005: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.516: I/Choreographer(892): Skipped 66 frames!  The application may be doing too much work on its main thread.
05-11 08:59:57.516: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.522: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.542: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.572: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.994: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:57.994: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.032: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.052: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.332: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.352: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.352: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.352: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.372: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.392: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.462: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.472: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.702: E/AudioRecord(892): Unsupported configuration: sampleRate 256, format 1, channelMask 0x10
05-11 08:59:58.732: D/VS(892): Socket Created
05-11 08:59:58.802: I/Choreographer(892): Skipped 144 frames!  The application may be doing too much work on its main thread.
05-11 08:59:58.802: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.836: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:58.842: W/dalvikvm(892): threadid=15: thread exiting with uncaught exception (group=0x40a70930)
05-11 08:59:58.853: E/AndroidRuntime(892): FATAL EXCEPTION: Thread-97
05-11 08:59:58.853: E/AndroidRuntime(892): java.lang.NegativeArraySizeException: -2
05-11 08:59:58.853: E/AndroidRuntime(892):  at com.example.encryptechat.MainActivity$3.run(MainActivity.java:96)
05-11 08:59:58.853: E/AndroidRuntime(892):  at java.lang.Thread.run(Thread.java:856)
05-11 08:59:59.022: W/Trace(892): Unexpected value from nativeGetEnabledTags: 0
05-11 08:59:59.022: W/Trace(892): Unexpected value from 
    05-11 09:00:04.662: I/Process(892): Sending signal. PID: 892 SIG: 9

1 个答案:

答案 0 :(得分:0)

在android中,

应用程序出现错误:Application has stopped unexpectedly appears,当某个null pointer exception传递给null value的变量时。

为了阻止应用程序崩溃,首选使用try-catch block(通过捕获异常/错误来保护您的应用免受崩溃),

try {
    //do your stuff
} catch(Exception e) {
    Log.d("Error", e.getMessage());//to write a log for the exception/error
}

使用try-catch block

有一些优势
  1. 通过提供消息Application has stopped unexpectedly
  2. ,您的应用不会崩溃
  3. 您将在catch块中获得错误/异常
  4.   

    重要提示:

         

    始终尝试阅读LogCat消息,以便您了解其中的位置   当app正在运行时,你会得到什么类型的错误。