简体   繁体   English

Android Studio MainActivity.java 中的多个 onCreate 方法

[英]Android Studio Multiple onCreate methods in MainActivity.java

For a school project I am creating a simple app.对于学校项目,我正在创建一个简单的应用程序。 However, I run into one problem to finalize the app.但是,我在完成应用程序时遇到了一个问题。

I've been trying to make two buttons on my main layout.我一直试图在我的主要布局上制作两个按钮。 The buttons are supposed to open a second layout, one called barcode_scanner.xml and another called vragen.xml这些按钮应该打开第二个布局,一个叫barcode_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
        //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);
                }
            });
        }




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








public class bovenbouw extends MainActivity{

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

I don't see any problems in the code.我在代码中没有看到任何问题。 What am I possibly doing wrong?我可能做错了什么? Thank you.谢谢。

Do not make multiple onCreate methods for the same activity.不要为同一个活动创建多个onCreate方法。 Handle all your button clicks in the same onCreate method.在同一个onCreate方法中处理所有按钮点击。 What you can do is this:你可以做的是:

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
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 next1 = (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);
        }
    });

    Button next2 = (Button) findViewById(R.id.bovenbutton);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(view.getContext(), Vragen.class);
            startActivityForResult(myIntent, 0);}

        });
    }
}

You don't need 2 onCreate() methods for two buttons.两个按钮不需要 2 个onCreate()方法。 As both of them are in the same layout, you can set onClickListener on both of them in one method only.由于它们都在同一布局中,因此您只能通过一种方法在它们两个上设置onClickListener

public class MainActivity extends AppCompatActivity {

    @Override
    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(this, BarcodeScanner.class);
                startActivityForResult(myIntent, 0);
            }
        });

        Button next1 = (Button) findViewById(R.id.bovenbutton);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(this, Vragen.class);
                startActivityForResult(myIntent, 1); 
                //Make sure the second parameter is not 0, so that you can differentiate between them in onActivityReult method.
            }
        });
    }
}

You don't have to create another class for vragen.class after bovenbutton is pressed.您没有按下bovenbutton后创建另一个类的vragen.class。 just create another button for bovenbutton in the main class oncreate method besides the scanner_button除了scanner_button之外,只需在主类oncreate方法中为bovenbutton创建另一个按钮

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

You're not supposed to register two Activities in single .java file.您不应该在单个 .java 文件中注册两个活动。 You must create separate Activity.您必须创建单独的活动。

package com.example.photosapp

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

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

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