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?
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:
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:There are different attributes you could add, based on your specific needs, but is the basic set up.