Creating a new Intent results in a error - android

when i do the fallowing i get screamed on :
Button newGameButton = (Button) this.findViewById(R.id.newGameButton);
newGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(this,gameScreen.class));
}
});
apparently my this is not the one needed
how can i fix this err?

I think the problem is a scoping issue with your this in your onClick activity. In that instance this is referring to the onClick method not your Activity. Try changing it to this:
startActivity(new Intent(countryCityGameMenu.this,GameScreen.class));
That should adjust your scoping issue.

"get screamed on" is not that descriptive, so all anyone can do is guess at what your problem is. My guess is that you need to add the gameScreen class to the manifest file. Also, does the gameScreen class extend Activity.
<activity android:name =".gameScreen" android:label="Name Of The Activity"/>

did you register your activity in the manifest.xml file?
if yes you could try to do
startActivity(new Intent(countryCityGameMenu.this,GameScreen.class));
because if you use this in the OnClickListener it doesn't refer to a countryCityGameMenu instance.

Are you sure you've named your class gameScreen and not GameScreen? The first letter in class names should be capital.

I think you need to get the context from the view (I dont have any Android developement environment at the moment)
public void onClick(View view) {
startActivity(new Intent(view.getContext(),gameScreen.class));
}

Related

How to create an activity with its corresponding XML file just tapping a button

I'm programming an app which one of its functionalities is that the users can tap on a plus button (typical (+)...) that has to create a new activity and a new XML file with a specific structure.
I'm beginner on Android and also in Stack Overflow, so due to that I'm unable to post images and make this question easier to understand.
I need you to summarize how to program this. I'm not telling you to codify my code, I just need to know if it's possible to do and ,more or less, the steps to get it.
Hope you can help me. Thanks!
Okay, so this is how you'd do it, if you're sure you want to create a new instance of the activity from that very same activity. First, make a reference to your "plus button". Assuming the android:id="#+id/plusButton, it'd be like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Initialize your layout and variables
findViewById(R.id.plusButton).setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
});
}
}
This would launch a new instance of the same Activity (MainActivity) and give it focus.
You can make the activity yourself and have a button open the activity.
You'd start by making a new android activity. With eclipse it's simply File >> New >> Other >> Android Activity and then just fill out the form and hit finish. Make sure your current project is open.
Draw the button in your xml file, make sure it has a unique Id to reference and your text is declared in your strings.xml file that should look like this.
<string name="strX">(x)</string>
then in your activity's xml file under your button make sure you have
android:text="#string/strX"
You can also reference this in the GUI in the properties window under text.
With the button code in your .java you could use OnClickListener and Intent and the code for the button would look something like this.
TextView buttonYourButton = (TextView) findViewById(R.id.ButtonYourButtonId);
Button pushYourButton = (Button) buttonPlay;
pushYourButton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
Intent nameOfIntent = new Intent(NameOfCurrentClass.this, NameOfNewClass.class);
startActivity(nameOfIntent);
}
});
R.id.ButtonYourButton is the Id you gave the button, and the .class is the name of the public class in that .java file. Like:
public class MainActivity { ...
Anyways, good luck I was where you were about a month ago. Don't forget to check out tutorials on Android development on youtube, there are about a million of them. Also you can search stackoverflow for questions that already have been asked.

Main Activity Not Found Error in One Activity But not The other?

I have come across a strange error when i press a button that i supposed to return to teh main activity i get an activity not found error. But when i dod it from another activity, it works fine.
Working call:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Broken Call:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Campaign.this, Menu.class);
i.putExtra("Token", player.tokens);
// intent.putExtra("Round", player.round);
// intent.putExtra("Rank", player.rank);
// intent.putExtra("Score", player.score);
// intent.putExtra("Sec", player.secondsTapped);
// intent.putExtra("Min", player.minutesTapped);
// intent.putExtra("Hour", player.hoursTapped);
// intent.putExtra("Day", player.daysTapped);
// intent.putExtra("LifeTap", player.tapsInLife);
// intent.putExtra("SecTap", player.tapsPerSec);
// intent.putExtra("TapRo", player.tapps);
startActivity(i);
}
});
They are pretty much identical, but somehow the second one does not work. The first one has a bridging class before it. I looked through my xml file and nothing is wrong there since it works in one of my activities. The exact error i get is:
Unable to fin explicit activity class {com.tap.tapalot/android.view.Menu};
Thank You for Your help :)
I think you may have used wrong imports, so that Menu class is not the Menu-class you expect.
Try to specify the Menu.class in your intent with full package-path.
Check you seem to be importing. You are importing android.view.Menu in the second call. This is not your Activity class.
You are importing the wrong android.view.Menu class. You should import your Menu Activity class.
make sure you define 'Menu Class' in your mainfest file? like this:
<activity
android:name="pakgae.name.your.Menu"
android:label="#string/title_activity_menu" >
</activity>

Android (student cw) in need of direction

public class Menu extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
//myIntent.setClassName("hello.World", "hello.World.mybuttonclick");
// myIntent.putExtra("com.android.samples.SpecialValue", "Hello, Joe!"); // key/value pair, where key needs current package prefix.
//startActivity(myIntent);
//Button myButton = (Button) findViewById(R.id.my_button);
super.onCreate(icicle);
setContentView(R.layout.main);
}
public void updateLayout(){
Intent myIntent = new Intent(Menu.this, mybuttonclick.class);
startActivity(myIntent);
// TextView sayHello = (TextView) findViewById(R.id.Hello);
}
}
Hey guys, I am a new android java student and we have to develop a simple hello world app.. I am finding some difficulty getting my onClick() activity to work, using android:Onclick in xml.. what i am trying to do is change the content view do display a simply a different layout and saying hello.. i am using setContentLayout to do this, every time i click said button tho the android app crashes out.. am i doing something wrong?
regards,
Stefan
When you set a click listener in xml you must have the method defined inside the activity you are clicking in. Lets say you set the onClick in xml to be "buttonClicked", you must create a method looking exactly like the one below.
public void buttonClicked(View view)
{
//Your code here
}
The thing to notice is that the method is a public void with only a single parameter of type View. XML defined click listeners must be like this to work. The view object in the example above is the view that was clicked.
You update layout function needs to read
public void updateLayout(View view)
In response to your question, there are a number of things that are issues causing the complication that you described. Let it first be said, that you don't have to do anything any particular way, provided that you make concessions for certain things. Android is a very flexible platform and Java, as an OOP language allows you to do things that many non OOP languages do not.
Whenever you create a "clickable" item, like a Button, if you want to have your program respond, you must have something "listen" to it. This is known as a Listener. In your case, you are looking for an OnClickListener. The OnClickListener does not have to be a part of the Activity necessarily. It just has to be a class that implements View.OnClickListener. Then, you have tell the setOnClickListener() method of the Button who its listener is. The following example shows what is necessary without your declaration in XML (but it is important).
class Menu extends Activity implements View.OnClickListener
{
public void onCreate(Bundle icicle)
{ setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML);
btn.setOnClickListener(this);
}
public void onClick(View view)
{ int id = view.getId();
if (id == R.id.BUTTON_ID_AS_DEFINED_BY_YOUR_XML)
updateLayout()//Do your Click event here
}
public void updateLayout()
{ //updateLayout code...
}
}
Something that needs to be noted is the OnClick() above. Every OnClickListener must use the same signature as theOnClick() That means itmust have the same return and same arguments even if it has a different name. For what you are trying to do (in XML), you have set your android:OnClick to updateLayout. This means that `updateLayout() must be declared as follows:
public void updateLayout(View view)
Now, getting the update method to actually work: While you provide your code, we don't actually know what errors you are getting. It is always much easier to solve a problem if we have a copy of the Logcat output that includes the error you are receiving. Once, we have that we can target your error specifically and I can edit my answer to include what you may additionally need.
FuzzicalLogic

Trying to start a new Activity using Intent

I'm new to Android Dev, so please help me out.
I'm trying to start a new activity after i press a button but nothing seems to work.
Here's my code:
public class viewInfo extends Activity {
private Button btn;
public TextView txt;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
btn=(Button)findViewById(R.id.buy);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent myIntent = new Intent(viewInfo.this, buyNow.class);
startActivity(myIntent);
}
});
}
I've also added this new activity in the Manifest but it keeps crushing after I press the button.
What am I doing wrong?
Misread the question initially (original answer below for completeness sake).
Make sure you have the activity you are calling defined in your manifest file:
Something like
<activity android:name=".buyNow" android:label="#string/app_name"></activity>
within the application tags would suffice.
Here's the original answer.
Assuming that you have the correct button ID - try this in your onclick:
Intent myIntent = new Intent(getApplicationContext(), buyNow.class);
startActivity(myIntent);
You could add a log message inside your onClick too, to make sure it is actually being called. You can see the log from logcat (run via adb logcat on the command line)
Try to make it:
startActivity(new Intent("[Here is your package name.what you declared in Manifest file]"));
For this you need to write to your manifest:
Hope it helps.
There may be a problem with your buyNow activity that is causing the error.
You really need to use logcat to trace the error. You can enable this by clicking the menu item:
Window -> Show View -> Other...
the selecting "LogCat" from the Android folder
You can simply do this:
startActivity(new Intent(getBaseContext(),Activity.class));
Afther you registred your activity in manifest:
<activity
android:name="com.example.ActivityName"
android:label="#string/app_name" >
</activity>

Android, How to Remove "NullPointerException" coming in onClick method

Here i wrote some code, and i want to call a method of other class from onClick() and getting NPE in "new Start().moreApplication(feedsId);" line..
final TextView feeds=(TextView)findViewById(R.id.more_feeds);
feeds.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Log.e("MoreChoices","onclick---------");
final int feedsId= feeds.getId();
Log.e("MoreChoices","onclick----After initialization of id--------");
new Start().moreApplication(feedsId);
Log.e("MoreChoices","onclick----------------After calling--------");
}
});
Start.java is my an other Activity class, and i have one in Start.java, and i want to call that method from other class with using this code..
Please tell why i am getting NPE in
"new Start().moreApplication(feedsId);"
line...
Thanks in Advance please guide me....
Please, take some time reading anddev book, a very good place to start android development.
Read pages 50+ and you will understand that your not doing the right thing to create activities and pass data from one to another.
You should
startActivity by using either an intent or the class of this activity
pass data from your activity to the new one using intent's extra parameters.
Regards,
Stéphane
replace this line
final TextView feeds=(TextView)findViewById(R.id.more_feeds);
by
final TextView feeds=(TextView)this.findViewById(R.id.more_feeds);
I hope you got solution .
I think u need to get the View ID which u have clicked the enhanced code look like
final TextView feeds=(TextView)findViewById(R.id.more_feeds);
feeds.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Log.e("MoreChoices","onclick---------");
final int feedsId= arg0.getId();
Log.e("MoreChoices","onclick----After initialization of id--------");
new Start().moreApplication(feedsId);
Log.e("MoreChoices","onclick----------------After calling--------");
}
});

Categories

Resources