【问题标题】:Java Android Eclipse R.idJava Android Eclipse R.id
【发布时间】:2013-01-24 23:04:10
【问题描述】:

我想使用蓝牙为 Android 制作一个简单的应用程序。 所以我在网上看了一些例子,我找到了一个。 但是我的MainActivity.java 中有一个错误。 只有一个错误,但在“控制台”中没有注意到; 它只是用红色下划线。 由于这个问题,我无法运行该项目。

这是我的主要:

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);
    }
}
}

这是布局中的*activity_main.xml*:

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

R.java中的id类

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

所以我的问题出在 out = (TextView) findViewById(R.id.out); 行。我已经清理了项目,我尝试在布局中创建一个新的 xml 文件,更改了名称,但没有。

【问题讨论】:

    标签: java android eclipse r.java-file


    【解决方案1】:

    return true; 移动到onCreateOptionsMenu() 方法的最后,使其位于右括号之前之前。 Eclipse 一定是在抱怨无法访问的代码。

    我还建议不要onCreateOptionsMenu() 中做任何其他事情,这应该仅用于制作选项菜单,而不是修改 Activity 布局的 UI 元素部分(在这种情况下,您的outTextView)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多