[Android Q&A, Help & Troubleshooting] dahansh:
hi,

I hvae very spcieal problem, i read all the post on multithreading in android, but still go the famuse error. but, i got in after the second run of the same thread for exmaple "thGetDevice" from the sourcecode;

hope u can help me.. thnx..

/** Variable definition*/
CheckBox cbBlueTooth;
BluetoothAdapter mBluetoothAdapter;
AlertDialog.Builder dialog;
ArrayAdapter<String> mArrayAdapter;
ListView MainListView;
ProgressBar prProgressbar;
Thread thGetDevices;
Thread thClearDevices;
Handler handler;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

/* Initial Handler */
handler=new Handler() {
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message msg) {
prProgressbar.incrementProgressBy(msg.arg1);
mArrayAdapter = (ArrayAdapter<String>)msg.obj;
mArrayAdapter.notifyDataSetInvalidated();
mArrayAdapter.notifyDataSetChanged();
prProgressbar.setVisibility(ProgressBar.INVISIBLE);

/******************************
*
* this line cuse to the trouble
*/

setListAdapter(mArrayAdapter);
thClearDevices=null;
thGetDevices = null;
}
};

/* Set Finalization true */
// System.runFinalizersOnExit(true);
setContentView(R.layout.main);

/* ListView Definition */
TextView tvHeader = new TextView(this);
MainListView = this.getListView();
MainListView.addHeaderView(tvHeader);
mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
//mArrayAdapter.setNotifyOnChange(true);
prProgressbar = (ProgressBar)findViewById(R.id.progressBar1);
prProgressbar.setVisibility(ProgressBar.INVISIBLE);

/* Get Bluetooth driver */
cbBlueTooth = (CheckBox)findViewById(R.id.cbTurnBlueTooth);
dialog = new AlertDialog.Builder(this);

/* initializing Bluetooth adapter **/
mBluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
/*ShowNotifMessage("Sorry..", "You Don't have Bluetooth Driver");
try {
Thread.sleep(3000);
android.os.Process.killProcess(android.os.Process.myPid());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/

}

// Initial the thGetDeviceThread and thClearDevices
//initGetDevicesThread();
//initClearDevicesThread();

/*Listener for Checkbox*/
cbBlueTooth.setOnCheckedChangeListener(new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub

if (isChecked && !mBluetoothAdapter.isEnabled()) {
prProgressbar.setVisibility(ProgressBar.VISIBLE);
buttonView.setEnabled(false);
BluetoothAdapter.getDefaultAdapter().enable();
initGetDevicesThread();
buttonView.setEnabled(true);
}
else if (!isChecked && mBluetoothAdapter.isEnabled()) {
prProgressbar.setVisibility(ProgressBar.VISIBLE);
buttonView.setEnabled(false);
BluetoothAdapter.getDefaultAdapter().disable();
initClearDevicesThread();
buttonView.setEnabled(true);
}
else
{
prProgressbar.setVisibility(ProgressBar.VISIBLE);
buttonView.setEnabled(false);
BluetoothAdapter.getDefaultAdapter().enable();
initGetDevicesThread();
buttonView.setEnabled(true);
}
}
});
}

/** Gets all paird devices and put them in the Listview */
private void initGetDevicesThread()
{
thGetDevices = new Thread(new Runnable() {

public void run() {
Message msg = handler.obtainMessage();
msg.obj = getDeviceList();
handler.sendMessage(msg);
}

});

(thGetDevices).start();

}

/** clear all paird devices and put them in the Listview */
private void initClearDevicesThread()
{
thClearDevices = new Thread(new Runnable() {

public void run() {
Message msg = handler.obtainMessage();
msg.obj = clearListOfDevices();
handler.sendMessage(msg);
}

});

(thClearDevices).start();

}

public ArrayAdapter<String> getDeviceList()
{
while(BluetoothAdapter.getDefaultAdapter().getState() != BluetoothAdapter.STATE_ON);
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();

// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName());
}
}

return mArrayAdapter;
}

private ArrayAdapter<String> clearListOfDevices()
{
while(BluetoothAdapter.getDefaultAdapter().getState() != BluetoothAdapter.STATE_OFF);
mArrayAdapter = new ArrayAdapter<String>(MainListView.getContext(), android.R.layout.simple_list_item_1);
return mArrayAdapter;
}

//** */
private void ShowNotifMessage(String strTitle,String strMsg)
{
dialog.setTitle(strTitle);
dialog.setMessage(strMsg);
dialog.show();
}