java - Android Studio 多个 onCreate 方法

标签 java android android-studio

我一直在尝试在我的主布局上制作两个按钮。这些按钮应该打开第二个布局,称为barcode_scanner.xml和vragen.xml

但是,只有第一个按钮才能打开扫描仪。第二个按钮不执行任何操作。

这是我当前来自 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);

                    }
                });
            }};
        }

这是 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);

                    }
                });
            }};
        }

id 为“scannerButton”的按钮工作正常,但 id 为“bovenbouw”的按钮

我没有看到代码中有任何错误。可能出了什么问题?

最佳答案

您应该在 MainActivity.onCreate(..) 方法中声明这两个按钮

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

然后注册您的 onClick 监听器以打开新的 Activity。

对于您的第二个 Activity,您将有一个自己的类 SecondActivity 及其自己的 onCreate 方法来处理事务。

要打开第二个 Activity,您只需打开一个新的 Intent,如下所示

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

希望这有帮助:)

关于java - Android Studio 多个 onCreate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37808301/

相关文章:

android - 查找尝试连接到 Android 设备的进程

android-studio - android studio 设备监视器错误 "Java has started but returned exit code = 13"

Android gradle 项目构建非常缓慢

Java 隐藏的 JTabbedPane

java - 将具有多个类的netbeans java应用程序转换为.exe

java - 安卓 : Searching items using there barcodes in ebay

java - 尝试连接当时不可用的远程设备时,蓝牙聊天应用程序出现“服务发现失败”错误

java - 为什么 Reflection API 会为现有的枚举构造函数抛出 NoSuchMethodException?

java - Jersey @GET 方法不适用于 2 个路径参数

双击而不是长按的Android上下文菜单