错误:已在此活动中定义onCreate(Bundle)

时间:2015-02-11 10:40:37

标签: java android

作为Android应用程序开发的新手,我试图学习使用Intent连接两个活动。我尝试了一本书中的代码。它不断抛出一个错误 - '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);
    }
}

3 个答案:

答案 0 :(得分:1)

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

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

}

然后在第二个活动中删除:

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

它会起作用。

答案 1 :(得分:1)

只需删除主要活动和新活动的第一个onCreate事件即可。你不需要两次

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

}

答案 2 :(得分:0)

只需将 NewActivity 更改为:

即可
public class NewActivity extends Activity {

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

一个类只能包含一个onCreate()方法。首先了解活动生命周期http://developer.android.com/training/basics/activity-lifecycle/starting.html