我正在制作一个android应用程序,我在代码的这一部分中感到困惑。在上一个类中,我集成了蓝牙并调用了startApp。我想在调用startApp时在主页上显示这些按钮。据我所知,这就是我该怎么做的?为什么我不能这么做?当我尝试这样做时,eclipse不允许我这样做。我如何更改代码,以便可以设置如下所示的主菜单开始页?
谢谢!!
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="85dip"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center" android:orientation="vertical">
<TextView
android:text="@string/mainTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="25dip"
android:textSize="24.5sp"/>
<!-- Patient Option -->
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/patientButton"
android:id="@+id/patientButton">
</Button>
<!-- Doctor Option -->
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/doctorButton"
android:layout_weight="1"
android:id="@+id/doctorButton">
</Button>
<!-- Exit Mode -->
<Button android:text="@string/exit"
android:layout_weight="1"
android:id="@+id/exit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<!-- About Mode -->
<Button android:text="@string/aboutButton"
android:layout_weight="1"
android:id="@+id/aboutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
package com.joshi.remotedoc;
import com.joshi.remotedoc.DeviceList;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class Remote_DocActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
//private static final String TAG = "Remote_Doc";
private static final String TAG = "BluetoothChat";
private static final boolean D = true;
private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
private static final int REQUEST_CONNECT_DEVICE_INSECURE = 2;
private static final int REQUEST_ENABLE_BT = 3;
private BluetoothAdapter mBluetoothAdapter = null;
private String mConnectedDeviceName = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(D) Log.e(TAG, "+++ ON CREATE +++");
setContentView(R.layout.main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}
}
@Override
public void onStart() {
super.onStart();
if(D) Log.e(TAG, "++ ON START ++");
// If BT is not on, request that it be enabled.
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
}
else {
startApp();
}
}
private void ensureDiscoverable() {
if(D) Log.d(TAG, "ensure discoverable");
if (mBluetoothAdapter.getScanMode() !=
BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
}
private void startApp(){
View Patient_Button = findViewById(R.id.patientButton);
Patient_Button.setOnClickListener(this);
Patient_Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent b = new Intent(this, Detailed_ModeActivity.class);
startActivity(b);
break;
}
}
);
View Doctor_Button = findViewById(R.id.doctorButton);
Doctor_Button.setOnClickListener(this);
View About_Option = findViewById(R.id.aboutButton);
About_Option.setOnClickListener(this);
View Exit_Option = findViewById(R.id.exit);
Exit_Option.setOnClickListener(this);
}
/*private final Handler mHandler = new Handler() {
@Override
public void handleData(Data data) {
}
}*/
private void connectDevice(Intent data, boolean secure) {
// Get the device MAC address
String address = data.getExtras()
.getString(DeviceList.EXTRA_DEVICE_ADDRESS);
// Get the BluetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device
//mChatService.connect(device, secure);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.patientButton:
Intent b = new Intent(this, Detailed_ModeActivity.class);
startActivity(b);
break;
case R.id.doctorButton:
Intent a = new Intent(this, Detailed_ModeActivity.class);
startActivity(a);
break;
case R.id.aboutButton:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
}
}
}发布于 2011-08-06 06:15:51
而不是:
Intent b = new Intent(this, Detailed_ModeActivity.class);试试这个:
Intent b = new Intent(getApplicationContext(), Detailed_ModeActivity.class);发布于 2011-08-06 04:17:12
我有几点可能会有帮助:
startApp方法吗?如果没有,你就不能在那里调用findViewById,因为这是Activity对象的一个方法。.setOnClickListener(this);,你的类实现OnClickListener了吗?如果您希望将其设置为视图的OnClickListener,则必须进行此设置
https://stackoverflow.com/questions/6962040
复制相似问题