2015-05-10 4 views
0

Когда я запускаю свое приложение, он неожиданно остановился и дает мне сильную ошибку при добавлении 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(); 
} 
} 

Пожалуйста, дайте мне надлежащего руководства решить мою ошибку. Заранее спасибо!

журнал кошка:

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 
+0

Можете ли вы добавить ошибку LogCat? Это помогает нам идентифицировать проблему. –

+0

Вы получаете 'java.lang.NegativeArraySizeException: -2'. Какой бы ни был массив в вашей строке номер 96, проверьте его размер. – Kunu

+0

Вы уверены? весь код - 80 строк. Не проблема API? Я искал его в Google. Я понял, что Api 17 имеет ошибку, поэтому показывает эту ошибку. не могли бы вы показать мне точную линию ошибок? – M0TRIX

ответ

0

В андроида,

Применение выдает ошибку: 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. Вы получите сообщение об ошибке/исключение в блоке поймать

Важное примечание:

Всегда старайтесь читать сообщения LogCat так что вы получите, чтобы узнать, где находится и какой тип ошибки вы получаете, в то время как приложение работает.

Смежные вопросы