Reputation: 55
I read on here how to go from the MainActivity to a second activity, unfortunately I still don't fully understand what's going on, because if I use the same code to go from the second activity to a third it won't work, so, yeah copying code without understanding is always a problem. So, in general what's the easiest most basic way to just go from activity to activity to activity. In other words for now I want let's say 5 activities I can jump back and forth through by pressing a button.
package com.example.human.hurdlesb;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity2Activity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
Button btnScreen3 = (Button) findViewById(R.id.btnScreen3);
btnScreen3.setOnClickListener(this);
}
it complains that "view cannot be applied to...('this' seems to be the problem although the same code works in the MainActivity class
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate ...
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
and overriding the superclass seems to be another problem...
@Override
public void onClick(View view) {
Log.i("click", "you clicked");
Intent i = new
Intent(MainActivity2Activity.this,MainActivity3Activity.class);
startActivity(i);
}
}
Upvotes: 0
Views: 458
Reputation: 7494
The code you have stated will not compile because MainActivity2Activity
needs to implement the View.OnClickListener
interface. The onClick()
method that you override comes from this interface and without implementing it, you cannot override the method. Hence your compilation failure.
Upvotes: 1