我收到了错误日志
12-23 14:30:47.830: W/dalvikvm(13750): threadid=10: thread exiting with uncaught exception (group=0x40a4f1f8)
12-23 14:30:47.830: E/AndroidRuntime(13750): FATAL EXCEPTION: Thread-446
12-23 14:30:47.830: E/AndroidRuntime(13750): java.lang.NullPointerException
12-23 14:30:47.830: E/AndroidRuntime(13750): at com.example.ctracker_bluetoothapp.IncomingData$2.run(IncomingData.java:189)
`enter code here`12-23 14:30:47.830: E/AndroidRuntime(13750): at `enter code here`java.lang.Thread.run(Thread.java:856)
12-23 14:30:48.280: D/dalvikvm(13750): GC_CONCURRENT freed 113K, 4% free 6819K/7047K, paused 3ms+6ms
12-23 14:30:49.240: D/OpenGLRenderer(13750): Flushing caches (mode 0)
12-23 14:30:49.810: D/OpenGLRenderer(13750): Flushing caches (mode 0)
12-23 14:30:50.370: D/OpenGLRenderer(13750): Flushing caches (mode 1)
12-23 14:32:14.770: I/Process(13750): Sending signal. PID: 13750 SIG: 9
我有这段代码
package com.example.ctracker_bluetoothapp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.widget.*;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.content.*;
public class IncomingData extends Activity
{
volatile boolean stopWorker;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
Handler handler=new Handler();
InputStream mmInStream;
OutputStream mmOutStream;
BluetoothAdapter mBluetoothAdapter;
Button btnCancel;
TextView systemId1;
TextView opMode1;
TextView systemId;
TextView opMode;
EditText Ups1;
EditText Mode1;
TextView Ups;
TextView Mode;
EditText Hybrid1;
EditText PHC1;
TextView Hybrid;
TextView PHC;
EditText GridCharging1;
EditText SolarCharging1;
TextView GridCharging;
TextView SolarCharging;
EditText APSS1;
EditText TPSS1;
TextView APSS;
TextView TPSS;
EditText BV1;
EditText SV1;
TextView BV;
TextView SV;
EditText Main1;
EditText Load1;
TextView Main;
TextView Load;
EditText Trip1;
EditText TCT1;
TextView Trip;
TextView TCT;
EditText Now1;
EditText TPSSValue1;
TextView Now;
TextView TPSSValue;
EditText DC1;
EditText LFBD1;
TextView DC;
TextView LFBD;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incoming_data);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
btnCancel=(Button)findViewById(R.id.btnCancel);
systemId1=(TextView)findViewById(R.id. systemId1);
opMode1=(TextView)findViewById(R.id.opMode1);
systemId=(TextView)findViewById(R.id. systemId);
opMode=(TextView)findViewById(R.id.opMode);
Ups1=(EditText)findViewById(R.id. Ups1);
Mode1=(EditText)findViewById(R.id.Mode1);
Ups=(TextView)findViewById(R.id. Ups);
Mode=(TextView)findViewById(R.id.Mode);
Hybrid1=(EditText)findViewById(R.id.Hybrid1);
PHC1=(EditText)findViewById(R.id.PHC1);
Hybrid=(TextView)findViewById(R.id.Hybrid);
PHC=(TextView)findViewById(R.id.PHC);
GridCharging1=(EditText)findViewById(R.id.GridCharging1);
SolarCharging1=(EditText)findViewById(R.id.SolarCharging1);
GridCharging=(TextView)findViewById(R.id.GridCharging);
SolarCharging=(TextView)findViewById(R.id.SolarCharging);
APSS1=(EditText)findViewById(R.id.APSS1);
TPSS1=(EditText)findViewById(R.id.TPSS1);
APSS=(TextView)findViewById(R.id.APSS);
TPSS=(TextView)findViewById(R.id.TPSS);
BV1=(EditText)findViewById(R.id.BV1);
SV1=(EditText)findViewById(R.id.SV1);
BV=(TextView)findViewById(R.id.BV);
SV=(TextView)findViewById(R.id.SV);
Main1=(EditText)findViewById(R.id.Main1);
Load1=(EditText)findViewById(R.id.Load1);
Main=(TextView)findViewById(R.id.Main);
Load=(TextView)findViewById(R.id.Load);
Trip1=(EditText)findViewById(R.id.Trip1);
TCT1=(EditText)findViewById(R.id.TCT1);
Trip=(TextView)findViewById(R.id.Trip);
TCT=(TextView)findViewById(R.id.TCT);
Now1=(EditText)findViewById(R.id.Now1);
TPSSValue1=(EditText)findViewById(R.id.TPSSValue1);
Now=(TextView)findViewById(R.id.Now);
TPSSValue=(TextView)findViewById(R.id.TPSSValue);
DC1=(EditText)findViewById(R.id.DCTime1);
LFBD1=(EditText)findViewById(R.id.LFBD1);
DC=(TextView)findViewById(R.id.DCTime);
LFBD=(TextView)findViewById(R.id.LFBD);
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
mBluetoothAdapter.disable();
}
});
Intent intent=getIntent();
boolean flag = intent.getBooleanExtra("CallSearchDevices", true);
if (flag)
{
try{
beginListenForData();
}catch(Exception e){Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();}
}
}
public void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 126; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
try{
String[] values=data.split(",");
systemId1.setText(values[0].substring(1));
opMode1.setText(values[1]);
Ups1.setText(values[2]);
Mode1.setText(values[3]);
Hybrid1.setText(values[4]);
PHC1.setText(values[5]);
GridCharging1.setText(values[6]);
SolarCharging1.setText(values[7]);
APSS1.setText(values[8]);
TPSS1.setText(values[9]);
BV1.setText(values[10]);
SV1.setText(values[11]);
Main1.setText(values[12]);
Load1.setText(values[13]);
Trip1.setText(values[14]);
TCT1.setText(values[15]);
Now1.setText(values[16]);
TPSSValue1.setText(values[17]);
DC1.setText(values[18]);
LFBD1.setText(values[19]);
}catch(Exception e){}
}//rum method
});//handler
}//if
else
{
readBuffer[readBufferPosition++] = b;
}
}///for loop
}//outer if
} //try
catch (IOException ex)
{
stopWorker = true;
}
}//while
}//outer run
});
workerThread.start();
}//End of function beginListenForData
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.incoming_data, menu);
return true;
}
}
当我尝试运行函数beginListenForData()
时,我得到了这个答案 0 :(得分:1)
mmInStream
为空,因为它未在任何地方初始化(第189行)。
提示:您可以从错误跟踪中了解导致问题的行
12-23 14:30:47.830: E/AndroidRuntime(13750): at com.example.ctracker_bluetoothapp.IncomingData$2.run(IncomingData.java:**189**)
答案 1 :(得分:0)
您的InputStream mmInStream
为空。你还没有初始化它。因此,当您运行函数NullPointerException
时,它会在行int bytesAvailable = mmInStream.available();
中抛出beginListenForData();
。所以,初始化它。