1

I've been trying to make a home page with several buttons that can be tapped to lead to different activities (classes, i'm working in Eclipse). I've got one button leading to an activity fine. On copying and pasting it and changing information to match the new layout and in editing the manifest, I keep getting errors. Either the app crashes or the button remains defunct. I've followed the links below and it's still not matching correctly:

Android - Creating a new activity in Eclipse

How to start new activity on button click

http://www.youtube.com/watch?v=cv2bh53IL_Y

All of which create errors when you duplicate the work. Here's what I have below:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Button userguide;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userguide = (Button)findViewById (R.id.userguide);
userguide.setOnClickListener (new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,userguide.class);
startActivity(intent);


            }
        });
}

{

Button worldinformation;

Intent startNewActivityOpen = new Intent(MainActivity.this, worldinformation.class);
startActivityForResult (startNewActivityOpen, 0);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
    }

}

Thanks!

Community
  • 1
  • 1

1 Answers1

1

Your new Intent line is a little off. Instead of the button (userguide) class you want to use the NewActivity you want to start class.

userguide.setOnClickListener (new View.OnClickListener() {

@Override
public void onClick(View v) {
    // CHANGE "NextActivity" to the class of the activity you want to start.
    Intent intent = new Intent(v.getContext(), NextActivity.class);
    startActivity(intent);


            }
        });
petey
  • 16,914
  • 6
  • 65
  • 97
  • This isn't working for me, I get syntax errors all over or i'm being asked to remove the @override, which is one of the errors i was having with my original. The onClick View also causes errors. – user2769265 Sep 11 '13 at 19:21
  • I just used your code above,... but this sort of thing depends on what java version you have set as the project language level, removing it should be ok if you are targeting java 5. Leave it in for version 6. – petey Sep 11 '13 at 19:23
  • I fixed all the errors but now when i click on the button it won't work for some reason, crashing the app: `Button worldinformation; protected void onCreate1(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userguide = (Button)findViewById (R.id.worldinformation); userguide.setOnClickListener (new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(v.getContext(), worldinformation.class); startActivity(intent); } }); }` – user2769265 Sep 11 '13 at 20:05
  • WHen creating an intent to start specific activity, the 2nd param MUST of an activity class. worldinformation is a button not an activity so you cant use that. – petey Sep 11 '13 at 20:13
  • Would you mind explaining that again? I have set worldinformation as an activity in the manifest. I also named the button that in the activity_main.java. worldinformation.xml is also a layout that i have set along with a corresponding java file. I'm struggling to see the issue; I would really appreciate further help! – user2769265 Sep 11 '13 at 20:24
  • what is `Button worldinformation`? (from the code block in the question above)? – petey Sep 11 '13 at 20:49
  • So you're saying remove that element? – user2769265 Sep 11 '13 at 21:02