0

Bluetoothを使ったAndroid用の簡単なアプリを作りたいです。それで、ウェブでいくつかの例を見て、見つけました。ただし、にエラーがありますMainActivity.java。エラーは 1 つだけですが、「コンソール」には表示されません。赤い下線が引かれているだけです。この問題により、プロジェクトを実行できません。

これは私のメインです:

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ファイルを作成しようとしましたが、名前を変更しましたが、何もしませんでした。

4

1 に答える 1

0

メソッドreturn true;の最後に移動して、閉じ中括弧の直前に配置します。Eclipseは到達不能コードについて不平を言っているに違いありません。onCreateOptionsMenu()

また、で他に何もしないことをお勧めしますonCreateOptionsMenu()。これは、オプションメニューを作成するためだけのものであり、アクティビティのレイアウトのUI要素部分(この場合はoutTextView)を変更するためのものではありません。

于 2013-01-24T23:10:40.003 に答える