简体   繁体   English

Android Studio的多个onCreate方法

[英]Android Studio multiple onCreate methods

I've been trying to make two buttong on my main layout. 我一直在尝试在主布局上添加两个buttong。 The buttons are supposed to open a second layout, called barcode_scanner.xml and vragen.xml 这些按钮应该打开第二个布局,分别称为条形码_scanner.xml和vragen.xml。

However, only the first button opens the scanner. 但是,只有第一个按钮可以打开扫描仪。 The second button does not do anything. 第二个按钮不执行任何操作。

This is my current code from MainActivity.java 这是我来自MainActivity.java的当前代码

package com.kvprasad.zbarbarcodescanner;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button scannerButton;

    //@Override
    //protected void onCreate(Bundle savedInstanceState) {
    //  super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    // scannerButton = (Button) findViewById(R.id.scannerButton);

    //scannerButton.setOnClickListener(new View.OnClickListener() {
    //  @Override
    // public void onClick(View v) {

//                Intent intent = new Intent(v.getContext(), BarcodeScanner.class);
    //              startActivity(intent);
    //        }
    //  });
    //}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    //Barcodescanner knop
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button next = (Button) findViewById(R.id.scannerButton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }




//bovenbouw knop
    public class AutoBodyActivity extends MainActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button next = (Button) findViewById(R.id.bovenbouw);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Vragen.class);
                startActivityForResult(myIntent, 0);

                    }
                });
            }};
        }

This is the specific part with the onCreate methods: 这是onCreate方法的特定部分:

@Override
    //Barcodescanner knop
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button next = (Button) findViewById(R.id.scannerButton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), BarcodeScanner.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }




//bovenbouw knop
    public class AutoBodyActivity extends MainActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button next = (Button) findViewById(R.id.bovenbouw);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Vragen.class);
                startActivityForResult(myIntent, 0);

                    }
                });
            }};
        }

The button with id: 'scannerButton' works fine, but the button with id: 'bovenbouw' 标识为“ scannerButton”的按钮可以正常工作,但标识为“ bovenbouw”的按钮

I don't see any mistakes in the code. 我没有在代码中看到任何错误。 What is possibly wrong? 可能是什么问题?

You should declare both buttons in the MainActivity.onCreate(..) method 您应该在MainActivity.onCreate(..)方法中声明两个按钮

Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);

and then register your onClick listeners to open a new Activity. 然后注册您的onClick侦听器以打开一个新的活动。

For your second activity you'd have an own class SecondActivity with its own onCreate method to take care of stuff. 对于第二个活动,您将拥有一个自己的类SecondActivity及其自己的onCreate方法来处理所有事情。

To open the second Activity you just open a new Intent like so 要打开第二个活动,只需像这样打开一个新的Intent

Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent)

hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM