我已使用developer.android.com已提供的蓝牙聊天代码。我很难搞清楚我在代码中出错的地方。它假设在DeviceList
类上调用一个Dialog,但每次我点击操作栏上的Find Devices
按钮。该应用程序崩溃。我有这个错误java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
这是来自我的logcat。我不知道为什么这应该是应用程序崩溃的原因。请帮忙
logcat的
08-23 11:34:31.237: V/ActivityThread(3440): Performing resume of ActivityRecord{420f26b8 token=android.os.BinderProxy@420f1e90 {com.project.tsateacher/bluetooth.DeviceListActivity}} finished=false
08-23 11:34:31.238: D/ActivityThread(3440): ACT-AM_ON_RESUME_CALLED ActivityRecord{420f26b8 token=android.os.BinderProxy@420f1e90 {com.project.tsateacher/bluetooth.DeviceListActivity}}
08-23 11:34:31.238: V/ActivityThread(3440): Resume ActivityRecord{420f26b8 token=android.os.BinderProxy@420f1e90 {com.project.tsateacher/bluetooth.DeviceListActivity}} started activity: false, hideForNow: false, finished: false
08-23 11:34:31.239: V/PhoneWindow(3440): DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{420f5ee0 I.E..... R.....I. 0,0-0,0}
08-23 11:34:31.241: V/ViewRootImpl(3440): mApplyParallel = false, this = ViewRoot{421209e0 ,ident = 5}
08-23 11:34:31.245: V/ActivityThread(3440): Resuming ActivityRecord{420f26b8 token=android.os.BinderProxy@420f1e90 {com.project.tsateacher/bluetooth.DeviceListActivity}} with isForward=true
08-23 11:34:31.245: V/PhoneWindow(3440): DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{421209e0 com.project.tsateacher/bluetooth.DeviceListActivity,ident = 5}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{420f5ee0 V.E..... R.....I. 0,0-0,0}
08-23 11:34:31.246: V/ActivityThread(3440): Scheduling idle handler for ActivityRecord{420f26b8 token=android.os.BinderProxy@420f1e90 {com.project.tsateacher/bluetooth.DeviceListActivity}}
08-23 11:34:31.247: D/ActivityThread(3440): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{420f26b8 token=android.os.BinderProxy@420f1e90 {com.project.tsateacher/bluetooth.DeviceListActivity}}
08-23 11:34:31.265: D/ListView(3440): measureHeightOfChildren adapter=android.widget.ArrayAdapter@4211a940, startPosition=0, endPosition=5, maxHeight=776, this=android.widget.ListView{4210a4c8 VFED.VC. ......I. 0,0-0,0 #7f090029 app:id/paired_devices}
08-23 11:34:31.267: E/ArrayAdapter(3440): You must supply a resource ID for a TextView
08-23 11:34:31.275: D/AndroidRuntime(3440): Shutting down VM
08-23 11:34:31.275: D/dalvikvm(3440): threadid=1: detach (group=0x4187fce0)
08-23 11:34:31.275: W/dalvikvm(3440): threadid=1: thread exiting with uncaught exception (group=0x4187fce0)
08-23 11:34:31.275: W/dalvikvm(3440): threadid=1: uncaught exception occurred
08-23 11:34:31.276: W/System.err(3440): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
08-23 11:34:31.282: W/System.err(3440): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
08-23 11:34:31.282: W/System.err(3440): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-23 11:34:31.282: W/System.err(3440): at android.widget.AbsListView.obtainView(AbsListView.java:2338)
08-23 11:34:31.282: W/System.err(3440): at android.widget.ListView.measureHeightOfChildren(ListView.java:1273)
08-23 11:34:31.282: W/System.err(3440): at android.widget.ListView.onMeasure(ListView.java:1182)
08-23 11:34:31.282: W/System.err(3440): at android.view.View.measure(View.java:16875)
08-23 11:34:31.283: W/System.err(3440): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5394)
08-23 11:34:31.283: W/System.err(3440): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
08-23 11:34:31.283: W/System.err(3440): at android.widget.LinearLayout.measureVertical(LinearLayout.java:742)
08-23 11:34:31.283: W/System.err(3440): at android.widget.LinearLayout.onMeasure(LinearLayout.java:607)
08-23 11:34:31.283: W/System.err(3440): at android.view.View.measure(View.java:16875)
08-23 11:34:31.283: W/System.err(3440): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5394)
08-23 11:34:31.283: W/System.err(3440): at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
08-23 11:34:31.283: W/System.err(3440): at android.view.View.measure(View.java:16875)
08-23 11:34:31.283: W/System.err(3440): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5394)
08-23 11:34:31.283: W/System.err(3440): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1621)
08-23 11:34:31.284: W/System.err(3440): at android.widget.LinearLayout.measureVertical(LinearLayout.java:742)
08-23 11:34:31.284: W/System.err(3440): at android.widget.LinearLayout.onMeasure(LinearLayout.java:607)
08-23 11:34:31.284: W/System.err(3440): at android.view.View.measure(View.java:16875)
08-23 11:34:31.284: W/System.err(3440): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5394)
08-23 11:34:31.284: W/System.err(3440): at android.widget.FrameLayout.onMeasure(FrameLayout.java:340)
08-23 11:34:31.284: W/System.err(3440): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2504)
08-23 11:34:31.284: W/System.err(3440): at android.view.View.measure(View.java:16875)
08-23 11:34:31.284: W/System.err(3440): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2403)
08-23 11:34:31.284: W/System.err(3440): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1369)
08-23 11:34:31.284: W/System.err(3440): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1640)
08-23 11:34:31.284: W/System.err(3440): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
08-23 11:34:31.284: W/System.err(3440): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6412)
08-23 11:34:31.284: W/System.err(3440): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
08-23 11:34:31.284: W/System.err(3440): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
08-23 11:34:31.284: W/System.err(3440): at android.view.Choreographer.doFrame(Choreographer.java:560)
08-23 11:34:31.284: W/System.err(3440): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
08-23 11:34:31.285: W/System.err(3440): at android.os.Handler.handleCallback(Handler.java:808)
08-23 11:34:31.285: W/System.err(3440): at android.os.Handler.dispatchMessage(Handler.java:103)
08-23 11:34:31.285: W/System.err(3440): at android.os.Looper.loop(Looper.java:193)
08-23 11:34:31.285: W/System.err(3440): at android.app.ActivityThread.main(ActivityThread.java:5334)
08-23 11:34:31.285: W/System.err(3440): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 11:34:31.285: W/System.err(3440): at java.lang.reflect.Method.invoke(Method.java:515)
08-23 11:34:31.286: W/System.err(3440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
08-23 11:34:31.286: W/System.err(3440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
08-23 11:34:31.286: W/System.err(3440): at dalvik.system.NativeStart.main(Native Method)
08-23 11:34:31.287: W/System.err(3440): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
08-23 11:34:31.289: W/System.err(3440): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
08-23 11:34:31.289: W/System.err(3440): ... 40 more
08-23 11:34:31.289: W/dalvikvm(3440): threadid=1: calling UncaughtExceptionHandler
08-23 11:34:31.298: E/AndroidRuntime(3440): FATAL EXCEPTION: main
08-23 11:34:31.298: E/AndroidRuntime(3440): Process: com.project.tsateacher, PID: 3440
08-23 11:34:31.298: E/AndroidRuntime(3440): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
08-23 11:34:31.298: E/AndroidRuntime(3440): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
08-23 11:34:31.298: E/AndroidRuntime(3440): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-23 11:34:31.298: E/AndroidRuntime(3440): at android.widget.AbsListView.obtainView(AbsListView.java:2338)
08-23 11:34:31.298: E/AndroidRuntime(3440): at android.widget.ListView.measureHeightOfChildren
DeviceListActivity.java
public class DeviceListActivity extends Activity {
// 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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setup the window
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.bluetooth_device_list);
// 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.bluetooth_device_name);
mNewDevicesArrayAdapter = new ArrayAdapter<String>(this,
R.layout.bluetooth_device_name);
// Find and set up the ListView for paired devices
ListView pairedListView = (ListView) findViewById(R.id.paired_devices);
pairedListView.setAdapter(mPairedDevicesArrayAdapter);
pairedListView.setOnItemClickListener(mDeviceClickListener);
// Find and set up the ListView for newly discovered devices
ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
newDevicesListView.setOnItemClickListener(mDeviceClickListener);
// 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 on-click listener for all devices in the ListViews
private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
// Cancel discovery because it's costly and we're about to connect
mBtAdapter.cancelDiscovery();
// Get the device MAC address, which is the last 17 chars in the
// View
String info = ((TextView) v).getText().toString();
String address = info.substring(info.length() - 17);
// Create the result Intent and include the MAC address
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
// Set result and finish this Activity
setResult(Activity.RESULT_OK, intent);
finish();
}
};
// 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());
}
// When discovery is finished, change the Activity title
} 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);
}
}
}
};
}
单击搜索设备的图标会调用DeviceListActivty类,但它会崩溃。
答案 0 :(得分:0)
基本上,由于我看不到您的代码,但它似乎是由您定义arrayadpter的方式引起的,您需要将Textview添加到adpter,例如:
ArrayList<String> Test_list = new ArrayList<String>();
listAdapter = new ArrayAdapter<String>(this, R.layout.AAA,R.id.test,
Test_List);
AAA.xml:
<TextView
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</TextView>
如果您使用:
listAdapter = new ArrayAdapter<String>(this, R.layout.AAA,
Test_List);//without the textview info (R.id.test)
您可能会收到此错误。