这是我的应用代码:
代码:
mainMenu();
现在,当我运行我的代码时,我的应用程序崩溃,发出此错误:
public class MainActivity extends Activity {
private ArrayList<BluetoothDevice> foundDevices;
private ArrayAdapter<BluetoothDevice> aa;
private Handler handler = new Handler();
private ListView list;
private static int DISCOVERY_REQUEST = 1;
private BluetoothAdapter bluetooth;
private BluetoothSocket socket;
private UUID uuid = UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the Bluetooth Adapter
configureBluetooth();
// Setup the ListView of discovered devices
setupListView();
// Setup search button
setupSearchButton();
// Setup listen button
setupListenButton();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void configureBluetooth() {
bluetooth = BluetoothAdapter.getDefaultAdapter();
}
private void switchUI() {
final TextView messageText = (TextView)findViewById(R.id.text_messages);
final EditText textEntry = (EditText)findViewById(R.id.text_message);
messageText.setVisibility(View.VISIBLE);
list.setVisibility(View.GONE);
textEntry.setEnabled(true);
textEntry.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {
sendMessage(socket, textEntry.getText().toString());
textEntry.setText("");
return true;
}
return false;
}
});
BluetoothSocketListener bsl = new BluetoothSocketListener(socket,
handler, messageText);
Thread messageListener = new Thread(bsl);
messageListener.start();
}
private void setupListenButton() {
Button listenButton = (Button)findViewById(R.id.button_listen);
listenButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent disc;
disc = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(disc, DISCOVERY_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data){
if (requestCode == DISCOVERY_REQUEST) {
boolean isDiscoverable = resultCode > 0;
if (isDiscoverable) {
String name = "bluetoothserver";
try {
final BluetoothServerSocket btserver =
bluetooth.listenUsingRfcommWithServiceRecord(name, uuid);
AsyncTask<Integer, Void, BluetoothSocket> acceptThread =
new AsyncTask<Integer, Void, BluetoothSocket>() {
@Override
protected void onPostExecute(BluetoothSocket result) {
if (result != null)
switchUI();
}
@Override
protected BluetoothSocket doInBackground(Integer... params) {
// TODO Auto-generated method stub
try {
socket = btserver.accept(params[0]*1000);
return socket;
} catch (IOException e) {
Log.d("BLUETOOTH", e.getMessage());
}
return null;
}
};
acceptThread.execute(resultCode);
} catch (IOException e) {
Log.d("BLUETOOTH", e.getMessage());
}
}
}
}
private void setupListView() {
aa = new ArrayAdapter<BluetoothDevice>(this,
android.R.layout.simple_list_item_1,
foundDevices);
list = (ListView)findViewById(R.id.list_discovered);
list.setAdapter(aa);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int index, long arg3) {
AsyncTask<Integer, Void, Void> connectTask =
new AsyncTask<Integer, Void, Void>() {
@Override
protected void onPostExecute(Void result) {
switchUI();
}
@Override
protected Void doInBackground(Integer... params) {
// TODO Auto-generated method stub
try {
BluetoothDevice device = foundDevices.get(params[0]);
socket = device.createRfcommSocketToServiceRecord(uuid);
socket.connect();
} catch (IOException e) {
Log.d("BLUETOOTH_CLIENT", e.getMessage());
}
return null;
}
};
connectTask.execute(index);
}
});
}
private void setupSearchButton(){
Button searchButton = (Button)findViewById(R.id.button_search);
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
registerReceiver(discoveryResult,
new IntentFilter(BluetoothDevice.ACTION_FOUND));
if (!bluetooth.isDiscovering()) {
foundDevices.clear();
bluetooth.startDiscovery();
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
BroadcastReceiver discoveryResult = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
BluetoothDevice remoteDevice;
remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (bluetooth.getBondedDevices().contains(remoteDevice)) {
foundDevices.add(remoteDevice);
aa.notifyDataSetChanged();
}
}
};
private void sendMessage(BluetoothSocket socket, String msg) {
OutputStream outStream;
try {
outStream = socket.getOutputStream();
byte[] byteString = (msg + " ").getBytes();
int[] stringAsBytes=null;
stringAsBytes[byteString.length - 1] = 0;
outStream.write(byteString);
} catch (IOException e) {
Log.d("BLUETOOTH_COMMS", e.getMessage());
}
}
private class MessagePoster implements Runnable {
private TextView textView;
private String message;
public MessagePoster(TextView textView, String message) {
this.textView = textView;
this.message = message;
}
public void run() {
textView.setText(message);
}
}
private class BluetoothSocketListener implements Runnable {
private BluetoothSocket socket;
private TextView textView;
private Handler handler;
public BluetoothSocketListener(BluetoothSocket socket,
Handler handler, TextView textView) {
this.socket = socket;
this.textView = textView;
this.handler = handler;
}
public void run() {
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
try {
InputStream instream = socket.getInputStream();
int bytesRead = -1;
String message = "";
while (true) {
message = "";
bytesRead = instream.read(buffer);
if (bytesRead != -1) {
while ((bytesRead==bufferSize)&&(buffer[bufferSize-1] != 0)) {
message = message + new String(buffer, 0, bytesRead);
bytesRead = instream.read(buffer);
}
message = message + new String(buffer, 0, bytesRead - 1);
handler.post(new MessagePoster(textView, message));
socket.getInputStream();
}
}
} catch (IOException e) {
Log.d("BLUETOOTH_COMMS", e.getMessage());
}
}
}
}
现在我无法找到此错误的来源以及如何删除此错误! 这是一个使用蓝牙的聊天应用程序! 有什么帮助吗?
谈论初始化它是在这里代码处完成的:
10-21 13:05:24.604: E/AndroidRuntime(23046): FATAL EXCEPTION: main
10-21 13:05:24.604: E/AndroidRuntime(23046): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bluetoothtexting/com.example.bluetoothtexting.MainActivity}: java.lang.NullPointerException
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.ActivityThread.access$600(ActivityThread.java:162)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.os.Handler.dispatchMessage(Handler.java:107)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.os.Looper.loop(Looper.java:194)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.ActivityThread.main(ActivityThread.java:5371)
10-21 13:05:24.604: E/AndroidRuntime(23046): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 13:05:24.604: E/AndroidRuntime(23046): at java.lang.reflect.Method.invoke(Method.java:525)
10-21 13:05:24.604: E/AndroidRuntime(23046): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-21 13:05:24.604: E/AndroidRuntime(23046): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-21 13:05:24.604: E/AndroidRuntime(23046): at dalvik.system.NativeStart.main(Native Method)
10-21 13:05:24.604: E/AndroidRuntime(23046): Caused by: java.lang.NullPointerException
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.widget.ListView.setAdapter(ListView.java:495)
10-21 13:05:24.604: E/AndroidRuntime(23046): at com.example.bluetoothtexting.MainActivity.setupListView(MainActivity.java:151)
10-21 13:05:24.604: E/AndroidRuntime(23046): at com.example.bluetoothtexting.MainActivity.onCreate(MainActivity.java:54)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.Activity.performCreate(Activity.java:5122)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
10-21 13:05:24.604: E/AndroidRuntime(23046): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
10-21 13:05:24.604: E/AndroidRuntime(23046): ... 11 more