onCreate2(android.os.Bundle) 从未使用过?
onCreate2(android.os.Bundle) is never used?
我正在尝试制作多个打开不同 URL 的按钮,但我 保持 收到相同的错误,指出 onCreate2(android.os.Bundle) 从未使用过。
有谁知道如何解决这个错误以及我可以做些什么来阻止它在未来发生,
谢谢
package saintbedeslytham.saintbedes;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class news extends ActionBarActivity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=742/Christmas+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
}
Button button2;
protected void onCreate2(Bundle savedInstanceState) { **<- Errors here**
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=737/Autumn+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
}
}
问题是没有人会调用onCreate2。
正如 Pokas 所说,onCreate
是一种在您的 Android 应用程序启动时由系统调用的方法。当您按下启动器中的图标以启动您的应用程序后,系统将立即调用此方法一次。它旨在放置用于设置您的应用程序的设置代码。
有关生命周期的更多信息,请看这里:http://developer.android.com/training/basics/activity-lifecycle/starting.html
要解决您面临的这个问题,您应该在某处调用 onCreate2
(不建议),或者将所有代码移动到 onCreate 中,它应该如下所示:
package saintbedeslytham.saintbedes;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class news extends ActionBarActivity {
Button button1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=742/Christmas+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=737/Autumn+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
}
}
我正在尝试制作多个打开不同 URL 的按钮,但我 保持 收到相同的错误,指出 onCreate2(android.os.Bundle) 从未使用过。
有谁知道如何解决这个错误以及我可以做些什么来阻止它在未来发生,
谢谢
package saintbedeslytham.saintbedes;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class news extends ActionBarActivity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=742/Christmas+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
}
Button button2;
protected void onCreate2(Bundle savedInstanceState) { **<- Errors here**
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=737/Autumn+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
}
}
问题是没有人会调用onCreate2。
正如 Pokas 所说,onCreate
是一种在您的 Android 应用程序启动时由系统调用的方法。当您按下启动器中的图标以启动您的应用程序后,系统将立即调用此方法一次。它旨在放置用于设置您的应用程序的设置代码。
有关生命周期的更多信息,请看这里:http://developer.android.com/training/basics/activity-lifecycle/starting.html
要解决您面临的这个问题,您应该在某处调用 onCreate2
(不建议),或者将所有代码移动到 onCreate 中,它应该如下所示:
package saintbedeslytham.saintbedes;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
public class news extends ActionBarActivity {
Button button1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=742/Christmas+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=737/Autumn+Newsletter+2014.pdf"));
startActivity(NameOfTheIntent);
}
});
}
}