2011-08-05 164 views
0

我正在製作一個android應用程序,我對這段代碼感到困惑。我正在整合藍牙,並在之前的課程中致電startApp。我想在調用startApp時在主頁面上顯示這些按鈕。從我的理解這是我會如何去做這件事?爲什麼我不能這樣做?當我嘗試這樣做時,eclipse不會讓我。我將如何更改代碼,以便我可以設置主菜單起始頁面,如下所示?Java基礎知識幫助

謝謝!

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

    } 


} 
+0

什麼是您的主要活動的佈局XML外觀喜歡? –

+0

主要xml高於 – YoKaGe

回答

1

取而代之的OnClickListener

Intent b = new Intent(this, Detailed_ModeActivity.class); 

試試這個:

Intent b = new Intent(getApplicationContext(), Detailed_ModeActivity.class); 
+0

該項目編譯,但我收到我的手機上的錯誤消息說應用程序已隨機關閉 – YoKaGe

+0

因此,錯誤是固定的,它編譯,現在有另一個錯誤。使用你的模擬器運行應用程序,當你看到堆棧跟蹤的錯誤。調試和跟蹤錯誤是發展的重要組成部分。 –

+0

它說的是關於登錄貓的一些東西並不真正知道那是什麼? – YoKaGe

2

我有幾個點,這可能有助於:

  1. 讀取錯誤的Eclipse給你的是一個很大的優勢,你的很多問題都會通過閱讀它解決。
  2. 您的startApp方法在您的活動?如果不是,則不能在那裏撥打findViewById,因爲這是Activity對象的一種方法。
  3. 你的代碼中有很多.setOnClickListener(this);,你的班級實施OnClickListener?它必須是,如果你希望它被設置爲你的意見
+0

完整活動等級高於 – YoKaGe

+0

你得到的錯誤是什麼? – MByD

+0

構造函數Intent(new View.OnClickListener(){},Class )未定義 – YoKaGe