0

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

new_devices 添加了 activity_main.xml

我是android应用程序开发的新手,请帮忙。

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>
4

2 回答 2

0

您有此错误,因为您没有 ID 为“new_devices”的 ListView。您的 setContentView 应该指向您的布局,例如 setContentView(R.layout.activity_main),并且您在此布局上的 ListView 应该具有类似 @+id/new_devices 的 id。

于 2013-11-03T13:30:24.400 回答
0

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

现在每个 id 都与一个 View 相关联,并且每当您使用该 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->Clean All Projects,然后选择 Projects->Build the Project。

我希望这有帮助!

于 2013-11-03T13:39:49.497 回答