![](/img/trans.png)
[英]Android Studio Multiple onCreate methods in MainActivity.java
[英]Android Studio multiple onCreate methods
我一直在嘗試在主布局上添加兩個buttong。 這些按鈕應該打開第二個布局,分別稱為條形碼_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);
}
});
}};
}
標識為“ scannerButton”的按鈕可以正常工作,但標識為“ bovenbouw”的按鈕
我沒有在代碼中看到任何錯誤。 可能是什么問題?
您應該在MainActivity.onCreate(..)
方法中聲明兩個按鈕
Button b1 = (Button) findViewById(R.id.b1);
Button b2 = (Button) findViewById(R.id.b2);
然后注冊您的onClick
偵聽器以打開一個新的活動。
對於第二個活動,您將擁有一個自己的類SecondActivity
及其自己的onCreate
方法來處理所有事情。
要打開第二個活動,只需像這樣打開一個新的Intent
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent)
希望這可以幫助 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.