Hello Everyone, i am developing an app on android to catch bluetooth,
and i am facing an error which i couldn't understand what its source
can be, the error is on the logcat as the following:
"02-13 13:02:11.538: E/AndroidRuntime(196):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{android.mgo.helloandroid/
android.mgo.helloandroid.BTDdetecetwithV7Activity}:
java.lang.UnsupportedOperationException: addView(View, LayoutParams)
is not supported in AdapterView"
My code is bellow, please tell me where the bug is coming from because
i can't see where this is coming from.
package android.mgo.helloandroid;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class BTDdetecetwithV7Activity extends Activity {
/** Called when the activity is first created. */
// Debugging
private static final String TAG = "DeviceListActivity";
private static final boolean D = true;
// Return Intent extra
public static String EXTRA_DEVICE_ADDRESS = "device_address";
// Member fields
private BluetoothAdapter mBtAdapter;
private ArrayAdapter<String> mPairedDevicesArrayAdapter;
private ArrayAdapter<String> mNewDevicesArrayAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setup the window
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
// Set result CANCELED in case the user backs out
setResult(Activity.RESULT_CANCELED);
// Initialize the button to perform device discovery
Button scanButton = (Button) findViewById(R.id.button_scan);
scanButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
doDiscovery();
v.setVisibility(View.GONE);
}
});
// Initialize array adapters. One for already paired devices
and
// one for newly discovered devices
mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
R.layout.main);
mNewDevicesArrayAdapter = new ArrayAdapter<String>(this,
R.layout.main);
// Find and set up the ListView for paired devices
ListView pairedListView = (ListView)
findViewById(R.id.paired_devices);
pairedListView.setAdapter(mPairedDevicesArrayAdapter);
// Find and set up the ListView for newly discovered devices
ListView newDevicesListView = (ListView)
findViewById(R.id.new_devices);
newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
// Register for broadcasts when a device is discovered
IntentFilter filter = new
IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, filter);
// Register for broadcasts when discovery has finished
filter = new
IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(mReceiver, filter);
// Get the local Bluetooth adapter
mBtAdapter = BluetoothAdapter.getDefaultAdapter();
// Get a set of currently paired devices
Set<BluetoothDevice> pairedDevices =
mBtAdapter.getBondedDevices();
// If there are paired devices, add each one to the
ArrayAdapter
if (pairedDevices.size() > 0) {
findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
for (BluetoothDevice device : pairedDevices) {
mPairedDevicesArrayAdapter.add(device.getName() + "\n"
+ device.getAddress());
}
} else {
String noDevices =
getResources().getText(R.string.none_paired).toString();
mPairedDevicesArrayAdapter.add(noDevices);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Make sure we're not doing discovery anymore
if (mBtAdapter != null) {
mBtAdapter.cancelDiscovery();
}
// Unregister broadcast listeners
this.unregisterReceiver(mReceiver);
}
/**
* Start device discover with the BluetoothAdapter
*/
private void doDiscovery() {
if (D) Log.d(TAG, "doDiscovery()");
// Indicate scanning in the title
setProgressBarIndeterminateVisibility(true);
setTitle(R.string.scanning);
// Turn on sub-title for new devices
findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
// If we're already discovering, stop it
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
// Request discover from BluetoothAdapter
mBtAdapter.startDiscovery();
}
// The BroadcastReceiver that listens for discovered devices
and
// changes the title when discovery is finished
private final BroadcastReceiver mReceiver = new
BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// If it's already paired, skip it, because it's been
listed already
if (device.getBondState() !=
BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() +
"\n" + device.getAddress());
}
} else if
(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices =
getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
};
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/title_paired_devices"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/title_paired_devices"
android:visibility="gone"
android:background="#666"
android:textColor="#fff"
android:paddingLeft="5dp"
/>
<ListView android:id="@+id/paired_devices"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:stackFromBottom="true"
android:layout_weight="1">
<TextView android:id="@+id/title_new_devices"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/title_other_devices"
android:visibility="gone"
android:background="#666"
android:textColor="#fff"
android:paddingLeft="5dp"
/>
<ListView android:id="@+id/new_devices"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:stackFromBottom="true"
android:layout_weight="2"
/></ListView>
<Button android:id="@+id/button_scan"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/button_scan"
/>
</LinearLayout>
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.mgo.helloandroid"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".BTDdetecetwithV7Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission
android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
and the full tomcat report is:
02-13 13:02:11.421: D/AndroidRuntime(196): Shutting down VM
02-13 13:02:11.421: W/dalvikvm(196): threadid=3: thread exiting with
uncaught exception (group=0x4001b188)
02-13 13:02:11.431: E/AndroidRuntime(196): Uncaught handler: thread
main exiting due to uncaught exception
02-13 13:02:11.538: E/AndroidRuntime(196): java.lang.RuntimeException:
Unable to start activity ComponentInfo{android.mgo.helloandroid/
android.mgo.helloandroid.BTDdetecetwithV7Activity}:
java.lang.UnsupportedOperationException: addView(View, LayoutParams)
is not supported in AdapterView
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2496)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2512)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.os.Handler.dispatchMessage(Handler.java:99)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.os.Looper.loop(Looper.java:123)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.ActivityThread.main(ActivityThread.java:4363)
02-13 13:02:11.538: E/AndroidRuntime(196): at
java.lang.reflect.Method.invokeNative(Native Method)
02-13 13:02:11.538: E/AndroidRuntime(196): at
java.lang.reflect.Method.invoke(Method.java:521)
02-13 13:02:11.538: E/AndroidRuntime(196): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-13 13:02:11.538: E/AndroidRuntime(196): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-13 13:02:11.538: E/AndroidRuntime(196): at
dalvik.system.NativeStart.main(Native Method)
02-13 13:02:11.538: E/AndroidRuntime(196): Caused by:
java.lang.UnsupportedOperationException: addView(View, LayoutParams)
is not supported in AdapterView
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.widget.AdapterView.addView(AdapterView.java:461)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:622)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.view.LayoutInflater.inflate(LayoutInflater.java:407)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-13 13:02:11.538: E/AndroidRuntime(196): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:
198)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.Activity.setContentView(Activity.java:1622)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.mgo.helloandroid.BTDdetecetwithV7Activity.onCreate(BTDdetecetwithV7Activity.java:
44)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1047)
02-13 13:02:11.538: E/AndroidRuntime(196): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2459)
02-13 13:02:11.538: E/AndroidRuntime(196): ... 11 more
02-13 13:02:11.622: I/dalvikvm(196): threadid=7: reacting to signal 3
02-13 13:02:11.622: I/dalvikvm(196): Wrote stack trace to '/data/anr/
traces.txt'
02-13 13:02:12.261: I/dalvikvm(196): threadid=7: reacting to signal 3
02-13 13:02:12.271: I/dalvikvm(196): Wrote stack trace to '/data/anr/
traces.txt'
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en