GeekedOut
GeekedOut

Reputation: 17185

Android button and Intent code not compiling

I am very new to Android development. I have this code:

Button btnLaunch;
btnLaunch=(Button)findViewById(R.id.btnLaunch);

and I have these imports:

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

But my Eclipse is highlighting the "btnLaunch" in red. Why is that? Should I manually edit one of the configuration XML files to let the system know about btnLaunch? How is it meant to work?

Also, when I try to create an Intent like this:

      Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
      CurrentActivity.this.startActivity(myIntent);

The CurrentActivity and NextActivity are not recognized, and the autoComplete does not offer me to import those. Am I missing a package?

Upvotes: 0

Views: 361

Answers (2)

ixx
ixx

Reputation: 32273

The first part is probably an error with layout xml. Make sure you are setting the correct layout first:

setContentView(R.layout.nameoflayout);

nameoflayout is the name of the xml.

Then you have to have a button there with the correct id. This id also has to be unique within all your layouts.

The second part - if you know where the classes are, just add the import statements manually. The process of doing this should help you at least identifying the reason why it doesn't work.

Upvotes: 1

Mike D
Mike D

Reputation: 4946

Which btnLaunch? You have 2, one is button, and the other is in integer. My guess (from the limited amount of code you put up) is you did not declare an ID for your button. Am I right in guessing the error highlighted is related to finViewById(R.id.btnLaunch

The button declaration shoud look something like:

<Button
    android:id="@+id/btnLaunch"
    .
    .
    . />

Whenever you are trying to reference soemthing declared in an XML file, you have to pay attention to the ID's. On a related note, if there is a problem with the XML file, Eclipse will not generate your R.java file, so also make sure there are no errors in the XML.

For the second part, in the Project folder, there is a file called Manifest.xml, in there, all the Activities used by your application need to be listed there. If you have added Activties after creating the project, you will have add their entry manually. It would look somethig like:

<acitvity
    name=".MyActivity" />

There are different attributes you could add, based on your specific needs, but is the basic set up.

Upvotes: 1

Related Questions