Bart
Bart

Reputation: 29

Add new activity in eclipse android

I started programming for android with Eclipse a few days ago However, I am stuck now.

I would like to add a new activity, so that i can add a new screen with new layout (an info screen about the app)

It is made so that when you press a specific menu button (help) it launches the help.xml activity and shows a new screen with some new words.

This succeeded one time, but I cant manage to do this another time.

It just gives me the standard white themed screen.

Here is the code to direct to the activity, made from the main.java:

@Override
public boolean onCreateOptionsMenu (Menu menu) {
    getMenuInflater().inflate (R.menu.menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.help:
        Intent intent = new Intent(this, help.class);
        startActivity(intent);
        break;
    case R.id.quit:
        finish();  
        break;
    }
}

I dont have code in the help.java, and I have made some buttons in help.xml

The 'quit' button works fine but the 'help' thing doesnt. I am also very unsure where it links to, as the previous time I had to both make a help.java and a help.xml activity.

I already noted the help activity in the manifest.xml

Upvotes: 0

Views: 459

Answers (3)

Tamás Cseh
Tamás Cseh

Reputation: 3090

Edited

help.xml is not an Activity, I think this is your layout file for the Activity. You must create a new class which extends Activity:

public class help extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
    }
}

If your Activity's name is "main" (as you described) where you start the new Activity, then you should change the Intent to this:

Intent intent = new Intent(main.this, help.class);
startActivity(intent);

Upvotes: 0

TNR
TNR

Reputation: 5869

You have to @Override the onCreate() in Help Activity.

See the following code:

public class Help extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate();
        setContentView(R.layout.help);
    }
}

Upvotes: 1

Pulkit Goyal
Pulkit Goyal

Reputation: 5664

You will have to create Help.java and sent the contentView in onCreate to R.layout.help.

public class Help extends Activity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.help);
     }

 }

You then need to create an activity node in AndroidManifest.xml that points to Help.java.

Upvotes: 0

Related Questions