gpt4 book ai didi

java - 问题在布局中创建了一个按钮(Android)?

转载 作者:行者123 更新时间:2023-12-01 15:30:38 25 4
gpt4 key购买 nike

我尝试在一个布局中使用两个按钮:

  • 一个笔记按钮,在下面工作
  • 计算器按钮(button10)

我在 super.oncreate 上收到此错误:

"The method onCreate(Bundle) is undefined for the type Object" 

ma​​in.java:

public class IzzynActivity extends Activity{

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button wg = (Button) findViewById(R.id.button1);
wg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(IzzynActivity.this, notes.class);
IzzynActivity.this.startActivity(myIntent);
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button wg = (Button) findViewById(R.id.button10);
wg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(IzzynActivity.this, calculator.class);
setResult(RESULT_OK, intent);
finish();
}
});

}


}
}
}

最佳答案

我不知道你做了什么,但你已经把你的代码弄乱了。我的假设是,您一直在复制教程而没有阅读实际发生的内容,因此并没有真正理解您在做什么。

您的代码应如下所示(未经测试,我刚刚输入,但这就是要点)。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// find the first button and set an on click listener
Button wg = (Button) findViewById(R.id.button1);
wg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(IzzynActivity.this, notes.class);
IzzynActivity.this.startActivity(myIntent);
}
});

// find the next button and set an on click listener
Button otherButton = (Button)findViewById(R.id.button10);
otherButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
Intent intent = new Intent(IzzynActivity.this, calculator.class);
setResult(RESULT_OK, intent);
finish();
}
});

}

关于java - 问题在布局中创建了一个按钮(Android)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9557278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com