I have two activity MainActivity.java
and ListActivity.java
.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.nextButton);
button.setOnClickListener(new AdapterView.OnClickListener(){
public void onClick(View view){
startAc(view);
}
});
}
public void startAc(View view){
Intent intent = new Intent(this, ListActivity.class);
startActivity(intent);
}
As soon as I click the button, the Emulator gives an error Application has stopped working.
Is the use of Intent inappropriate?
Try this code your wrong import your are import adapterview please import correct View.OnClickListener
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.nextButton);
button.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent = new Intent(getapplicationcontext(), ListActivity.class);
startActivity(intent);
}
});
}
Add Your Manifest this line after closing first activity
<activity android:name=".ListActivity.class"/>
try changing this
to MainActivity.this
. Also make sure every activity is defined in manifest
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.