Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android活动问题_Java_Android_Bluetooth_Oncreate_Onstart - Fatal编程技术网

Java Android活动问题

Java Android活动问题,java,android,bluetooth,oncreate,onstart,Java,Android,Bluetooth,Oncreate,Onstart,在我的主活动类中,我在哪里声明自定义按钮?它可以在onStart方法中使用,还是只在onCreate方法中使用 如果有任何帮助,我们将不胜感激 为了进一步说明我所说的:这是我的活动课 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(D) Log.e(TAG, "+++ ON CREATE +++");

在我的主活动类中,我在哪里声明自定义按钮?它可以在onStart方法中使用,还是只在onCreate方法中使用

如果有任何帮助,我们将不胜感激

为了进一步说明我所说的:这是我的活动课

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 startApp(){

        View Patient_Button = findViewById(R.id.patientButton);
        Patient_Button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            //case R.id.patientButton:
                //Intent b = new Intent(getApplicationContext(), Detailed_ModeActivity.class);
                //startActivity(b);
                //break;
                }
            }
        );
        View Doctor_Button = findViewById(R.id.doctorButton);
        Doctor_Button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent a = new Intent(getApplicationContext(), Detailed_ModeActivity.class);
                startActivity(a);
                //break;
                }
            }
        );
        View About_Option = findViewById(R.id.aboutButton);
        About_Option.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent c = new Intent(getApplicationContext(), About.class);
                startActivity(c);
                //break;
                }
            }
        );
*

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>

在Android中,我认为在onCreate()方法中声明按钮是常规做法。正如@Mike D所说,您还可以创建一个控制器并在onCreate()方法中实例化它。虽然您的问题不太清楚您在这方面遇到了什么问题,但似乎您只是在这些选择之间寻找答案—onStart()和onCreate()

比如说,

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button sampleButton = (Button) findViewById(R.id.sampleButton);
} 
在这种情况下,您将在
main.xml
文件中声明该按钮,该按钮的id将是
sampleButton


此外,为了便于将来参考,请查看本页上的,以了解Android活动生命周期和放置位置,这一点非常重要。这不仅会回答这个问题,而且会给你自己研究这个概念的空间

试着加快你的发展,这样你就不必太担心这些小细节了

这取决于您如何声明自定义按钮。在代码中或XML资源中。通常您会在onCreate()中声明它。也许你应该考虑为这个按钮做个控制器。在onCreate()中用所需的视图实例化控制器。能否发布您的
main.xml
布局文件?您到底遇到了什么问题?请详细说明链接中的内容。RoboGuice通过为您处理大多数初始化调用来加快开发速度。因此,在onCreate(…)方法中,您不需要为所有资源(如按钮等)查找dviewbyid(…)。Wiki有一个很好的示例和进一步的解释ahaa,我希望您编辑自己的答案,并将上述注释的上述内容附加到答案中。然后等待提问者接受你的答案或其他人投票。