Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法解析ListView类型的新\u设备_Android_Android Listview - Fatal编程技术网

Android 无法解析ListView类型的新\u设备

Android 无法解析ListView类型的新\u设备,android,android-listview,Android,Android Listview,我的Java代码在这里,错误显示在第行:ListView NewDeviceListView=(ListView)findViewById(R.id.new_devices) 新的\u设备添加到activity\u main.xml中 我是android应用程序开发新手,plz帮助 MainActivity.java package com.example.sample; All imports added here public class MainActivity extend

我的Java代码在这里,错误显示在第行:ListView NewDeviceListView=(ListView)findViewById(R.id.new_devices)

新的\u设备添加到activity\u main.xml中

我是android应用程序开发新手,plz帮助

MainActivity.java

package com.example.sample;

All imports added here     

public class MainActivity extends Activity implements View.OnClickListener{
    Button btn;
    BluetoothDevice BTdevice;
    BluetoothAdapter bluetoothAdapter;
    ArrayAdapter<String> btArrayAdapter;  /** Called when the activity is first created. */ 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        btn = new Button(this);
        btn.setOnClickListener(this);
        //setContentView(R.layout.activity_main);
        setContentView(btn);

    }

    public void onClick(View view)
    {
        //updateTime();
        final int REQUEST_ENABLE_BT = 1;
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(bluetoothAdapter != null)
        {
            //btArrayAdapter.clear();
            Log.d("ARRAY ADAPTER"," I AM INITIALIZED ");

            if (bluetoothAdapter.isDiscovering())
            {
                bluetoothAdapter.cancelDiscovery();
            }
            if (!bluetoothAdapter.isEnabled())
            {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
            updateTime();
            setContentView(R.layout.activity_main);
            bluetoothAdapter.startDiscovery();
            Log.d("Discovery","Completed Discovery");
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 
            Log.d("Intent Filter","After Intent filter call");
            registerReceiver(ActionFoundReceiver, filter);
            Log.d("Intent Filter","After Register recieve call");
        }
        else
        {
            AlertDialog alertdialog = new AlertDialog.Builder(this).create();
            alertdialog.setTitle("Bluetooth Adapter");
            alertdialog.setMessage("Bluetooth not supported in the device");

        }

        ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
        btArrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_main);
        newDevicesListView.setAdapter(btArrayAdapter);
        //btDevList.setAdapter(btArrayAdapter);
        // Register the BroadcastReceiver
        //registerReceiver(ActionFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));


    }

    private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver()
    {
        @Override  public void onReceive(Context context, Intent intent)
        {
            // TODO Auto-generated method stub
            Log.d("Broadcastreciever","Inside Broadcast reciever");
            String action = intent.getAction();
            Log.d("Broadcastreciever","After Getaction");

            if(BluetoothDevice.ACTION_FOUND.equals(action))
            {
                Log.d("Broadcastreciever","Inside Action found");
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Log.d("Broadcastreciever","After get Parcelable");
                btArrayAdapter.add(device.getName() + "\n" + device.getAddress()); //Found Crash here
                Log.d("Broadcastreciever","After get Device address");
                btArrayAdapter.notifyDataSetChanged();
                Log.d("Broadcastreciever","After notify data changed");
            }
        }
    };



Activity_Main.xml looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <ListView
        //android:id="@+id/new_devices"
        android:id="@+id/android:new_devices" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
    </ListView>
</RelativeLayout>
package com.example.sample;
所有的进口都添加到这里
公共类MainActivity扩展活动实现View.OnClickListener{
按钮btn;
蓝牙设备;
蓝牙适配器;
ArrayAdapter btArrayAdapter;/**在首次创建活动时调用。*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
btn=新按钮(此);
btn.setOnClickListener(此);
//setContentView(R.layout.activity_main);
setContentView(btn);
}
公共void onClick(视图)
{
//updateTime();
最终整数请求\启用\ BT=1;
bluetoothAdapter=bluetoothAdapter.getDefaultAdapter();
if(蓝牙适配器!=null)
{
//btArrayAdapter.clear();
Log.d(“阵列适配器”,“我已初始化”);
if(bluetoothAdapter.isDiscovering())
{
bluetoothAdapter.cancelDiscovery();
}
如果(!bluetoothAdapter.isEnabled())
{
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
updateTime();
setContentView(R.layout.activity_main);
bluetoothAdapter.startDiscovery();
日志d(“发现”,“完成的发现”);
IntentFilter筛选器=新的IntentFilter(BluetoothDevice.ACTION\u已找到);
Log.d(“意向过滤器”,“意向过滤器调用后”);
registerReceiver(ActionFoundReceiver,过滤器);
Log.d(“意图过滤器”、“寄存器接收调用后”);
}
其他的
{
AlertDialog AlertDialog=新建AlertDialog.Builder(this.create();
alertdialog.setTitle(“蓝牙适配器”);
alertdialog.setMessage(“设备中不支持蓝牙”);
}
ListView新设备ListView=(ListView)findViewById(R.id.new_设备);
btArrayAdapter=新的ArrayAdapter(这个,R.layout.activity_main);
setAdapter(btArrayAdapter);
//btDevList.setAdapter(btArrayAdapter);
//注册广播接收器
//registerReceiver(ActionFoundReceiver,新的IntentFilter(BluetoothDevice.ACTION_-FOUND));
}
private final BroadcastReceiver ActionFoundReceiver=new BroadcastReceiver()
{
@重写公共void onReceive(上下文、意图)
{
//TODO自动生成的方法存根
Log.d(“广播接收器”、“内部广播接收器”);
String action=intent.getAction();
Log.d(“BroadcastReceiver”、“After Getaction”);
if(BluetoothDevice.ACTION_FOUND.equals(ACTION))
{
日志d(“广播接收者”,“发现内部行动”);
BluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA\u设备);
Log.d(“广播接收器”,“收到包裹后”);
btArrayAdapter.add(device.getName()+“\n”+device.getAddress());//在此处发现崩溃
Log.d(“广播接收器”,“获取设备地址后”);
btArrayAdapter.notifyDataSetChanged();
Log.d(“广播接收器”,“通知数据更改后”);
}
}
};
Activity_Main.xml如下所示:

出现此错误是因为您没有id为“new_devices”的ListView。您的setContentView应该指向您的布局,就像setContentView(R.layout.activity_main)一样,并且您在此布局上的ListView应该具有类似@+id/new_设备的id。

android的工作方式是,每当您以xml创建id并构建项目时,它都会在R.java中添加这些id。然后您可以在类中访问相同的id

现在,每个id都与一个视图相关联,无论何时使用该id,都应确保视图已填充(这将避免NUllPointerException)

在您的代码中,不会填充上述视图,因为您没有在onCreate中使用setContentView。在访问任何视图之前,应使用setContentView(填充视图)

将onCreate更改为:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Rest of the code
    }
另外,在Activity_Main.xml 1)中,将名称更改为Activity_Main.xml(如果不是),然后更改ListView的代码,如下所示:

    <ListView
        android:id="@+id/new_devices"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true">
    </ListView>

保存XML,进入Project->清理所有项目,然后进入Project->构建项目


我希望这有帮助!

我已更改了setContentView(R.layout.activity_main)。尽管我的activity_main.xml中有@+id/new_设备,但它显示了一个错误。此布局xml是否有任何错误?这有助于解决问题。尽管我仍然无法在listview中查看BT设备列表。谢谢。