gpt4 book ai didi

java - 错误: onCreate(Bundle) is already defined in this activity

转载 作者:行者123 更新时间:2023-12-02 04:59:16 27 4
gpt4 key购买 nike

作为 Android 应用程序开发新手,我试图学习使用 Intent 连接两个 Activity 。我尝试了书中的代码。它不断抛出错误,指出“onCreate(Bundle)”已在 MainActivity 类和 NewActivity 类中定义。如果我能找到解决方案,将会有很大帮助。

MainActivity.class

public class MainActivity extends Activity {

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

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);

View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});

}

NewActivity.class

public class NewActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}
}

最佳答案

如果你想连接这些 Activity ,你必须这样做:

public class MainActivity extends Activity {

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

View view = getWindow().getDecorView().findViewById(android.R.id.content);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent startIntent = new Intent(MainActivity.this, NewActivity.class);
startActivity(startIntent);
}
});

}

然后在第二个 Activity 中删除:

  public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_new);
}

并复制此内容:

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

它会起作用的。

关于java - 错误: onCreate(Bundle) is already defined in this activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28452194/

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