简体   繁体   中英

Java Android Eclipse R.id

I would like make a simple application for Android using Bluetooth. So I looked upon some examples on the web and I've found one. However I have an error in my MainActivity.java . There is only one error but it is not noticed in the 'console'; It's just underlined in red. Due to this problem, I can't run the project.

This is my Main :

package com.testbluetooth;

import android.os.Bundle;
import com.testbluetooth.R;
import android.app.Activity;
import android.view.Menu;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.widget.TextView;

public class MainActivity extends Activity 
{
    TextView out;

    // Called when the activity is first created. 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

   @Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;

    //------here the error underlined by Eclipse: -------
    out = (TextView) findViewById(R.id.out);


    // Getting the Bluetooth adapter
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    out.append("\nAdapter: " + adapter);

    // Check for Bluetooth support in the first place 
    // Emulator doesn't support Bluetooth and will return null
    if(adapter==null) 
    { 
        out.append("\nBluetooth NOT supported. Aborting.");
        //return;
    }

    // Starting the device discovery
    out.append("\nStarting discovery...");
    adapter.startDiscovery();
    out.append("\nDone with discovery...");

    // Listing paired devices
    out.append("\nDevices Pared:");
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    for (BluetoothDevice device : devices) 
    {
        out.append("\nFound device: " + device);
    }
}
}

And this is *activity_main.xml* in the layout :

<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:id="@+id/out"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/app_name" />

</RelativeLayout>

The id class in R.java :

public static final class id {
    public static final int menu_settings=0x7f070001;
    public static final int out=0x7f070000;
 }

So It's with the line out = (TextView) findViewById(R.id.out); that I've the problem. I've cleaned the project, I tried to create a new xml file in the layout, change out's name, but nothing.

Move return true; to the very end of your onCreateOptionsMenu() method so it is just before its closing brace. Eclipse must be complaining about unreachable code.

I'd also recommend not doing anything else in onCreateOptionsMenu() , this should be only for making the options menu, not modifying a UI element part of the Activity's layout (in this case, your out TextView).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM