I'm trying to make a game for adroid and am having some problems with my windows.
I can open the "newgame" window from the "mainactivity", I'm trying to work on the "back" button, but I can't get it to work.
I also can't open new windows from the second "newgame" window (was testing if it would start to "load")
This may be a stupid mistake but I have no idea why its not working. Most app tutorials deal with one window and thus don't help me
My code:
mainactivity.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button NewGame = (Button) findViewById(R.id.button1);
NewGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.newgame);
}
});
final Button Load = (Button) findViewById(R.id.button2);
Load.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
newgame.java:
package dream.o.eternaty;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class NewGame extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgame);
final Button Back = (Button) findViewById(R.id.newgameback);
Back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.loadgame);
}
});
};
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}});
}
You shouldn't be transitioning the views, but actually starting a new activity for NewGame, as that's what you've declared it as, you can do it like this
Intent newIntent = new Intent(MainActivity.this,NewGame.class);
startActivityForResult(newIntent, 0);
in your MainActivity's onClick listener
Related
I am making an app in android Studio and if you open the app on your phone you see the Launcer activity. I have a button that sends you to a new activity where te game is located. Once i click the Start button, The app closes and doesn't goes to the other activity. Why is that?
This is my code of the Launcher activity:
package joenio.sirname;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class SirName_launcher extends AppCompatActivity {
public static Button button_start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sir_name_launcher);
StartButton();
}
public void StartButton(){
button_start = (Button) findViewById(R.id.button_start);
button_start.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent("joenio.sirname.Game");
startActivity(intent1);
}
}
);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_sir_name_launcher, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is the code of the second activity:
package joenio.sirname;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import android.widget.Toast;
public class Game extends AppCompatActivity {
public static EditText editText_surname;
public static TextView textView_name;
public static Button button_check;
int x =0; //to keep track of qustions
//Context editText_this = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Displayquestions();
}
public void Displayquestions(){
final ArrayList<String> mQuestionList = new ArrayList<>();
mQuestionList.add("1+2");
mQuestionList.add("6+8");
mQuestionList.add("5 * 6");
mQuestionList.add("8*5");
mQuestionList.add("6+16");
mQuestionList.add("18-5");
textView_displayquestion.setText((mQuestionList.get(x)));//displayquestion is textview
final ArrayList<String> mAnswerList=new ArrayList<>();
mAnswerList.add("3");
mAnswerList.add("14");
mAnswerList.add("30");
mAnswerList.add("40");
mAnswerList.add("22");
mAnswerList.add("13");
//button_check is the button when user click it will first check answer and than move to next question if answer is correct
button_check.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//editText_this;
String answer = editText_ans.getText().toString();
if (answer.equals(mAnswerList.get(x))) {
x = x + 1;
textView_displayquestion.setText(mQuestionList.get(x)); //answer is correct display next quesion
Toast.makeText(getApplication().getBaseContext(),
(R.string.Nice), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplication().getBaseContext(),
(R.string.tryagain), Toast.LENGTH_SHORT).show();
}
}
});
}
}
You are no where initializing your TextView and Button which must be causing NullPointerException.
Change your Game activity like this
TextView textView_displayquestion;
Button button_check;
EditText editText_ans;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
textView_displayquestion = (TextView)findViewById(R.id.displayquestion); //change as per your id
button_check = (Button)findViewById(R.id.buttoncheck); //change as per your id
editText_ans = (EditText)findViewById(R.id.answer); //change as per your id
Displayquestions();
}
In your button click , change the code as below.
Intent intent1 = new Intent(SirName_launcher.this, Game.class);
startActivity(intent1);
And also add the new Game Activity in your Manifest file too.
so I'm fairly new to Android. Lemme just explain first:
I'm trying to make a small portable music app, just for my phone and to test my skills/review what I've learned. I have the mainactivity setup to pick a song with 1 of 4 buttons, and it'll start another activity with buttons to pause, resume, and go back to the song selection screen (MainActivity). I'm trying to get the back button to both release the player and finish the activity, and I've tried many different things but nothing seems to work; the app closes due to some exception (a common one being NullPointerException).
So here's MainActivity:
package me.lemmy.portablemusic.app;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button wreckingBall, happy, lig, ligMulti;
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");
public static MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set buttons
wreckingBall = (Button) findViewById(R.id.song_wreck);
happy = (Button) findViewById(R.id.song_happy);
lig = (Button) findViewById(R.id.song_lig);
ligMulti = (Button) findViewById(R.id.song_lig_multi);
//listeners
wreckingBall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.wrecking_ball);
player.start();
}
});
happy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.happy);
player.start();
}
});
lig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go);
player.start();
}
});
ligMulti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(songPickedActivity);
player = MediaPlayer.create(MainActivity.this, R.raw.let_it_go_multi);
player.start();
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static MediaPlayer getPlayer(){
return player;
}
}
and here's my SongPicked activity:
package me.lemmy.portablemusic.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.media.MediaPlayer;
import android.widget.TextView;
/**
* Created by Lemuel on 6/20/14.
*/
public class SongPicked extends Activity {
TextView text;
Button pause, resume, back;
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songpicked);
text = (TextView) findViewById(R.id.tvPlaying);
pause = (Button) findViewById(R.id.buttonPause);
resume = (Button) findViewById(R.id.buttonResume);
back = (Button) findViewById(R.id.buttonBack);
player = MainActivity.getPlayer();
pause.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
player.pause();
}
});
resume.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
player.start();
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
player.release();
}
});
}
#Override
protected void onPause(){
super.onPause();
finish();
}
}
Any help is appreciated, thanks!
P.S. I know I can't use copyrighted music in my apps, this is just a test.
I suggest to first make sure the activity is in the AndroidManifest file. Second is to change your intent from this:
Intent songPickedActivity = new Intent("me.lemmy.portablemusic.app.SONGPICKED");
into:
Intent songPickedActivity = new Intent(this, SongPicked.class);
Do it as well on the songPickedActivity when going back to MainActivity. You can use putExtra to send data to next intent. For more information click this link.
I'm developing an android application and I'm using Eclipse ADT.
I want to get the content of a webpage that say only true or false
Here is my code of my MainActivity.java
package com.appmobirep;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addButtonClickListner();
}
public void addButtonClickListner()
{
Button btnLogin = (Button)findViewById(R.id.btnTest);
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Need to get the webpage content and set it to a string
String Data = "";
}
});
}
#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;
}
}
Can you please help me on this
Thank you...
Am new to Android so just practising on small stuff, what am trying to do is a button(B1) in the (mainactivity) layout and when I click it , it changes to another layout(main) which has another button (B2)which changes back to mainactivity layout out on-click , B1 is working okey , but B2 not working any ideas?
package com.example.android;
import java.util.Random;
import java.awt.*;
import com.example.android.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.clickme);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.main);
/*Random rand = new Random();
btn.setBackgroundColor(Color.argb(255, rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));*/
}
});
}
#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;
}
}
this is (main) layout
package com.example.android;
import com.example.android.R;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
Button btn;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.ChangeColor);
text = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
/*Random rand = new Random();
btn.setBackgroundColor(Color.argb(255, rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));
text.setTextColor(Color.argb(255, rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));*/
}
});
}
}
setContentView(R.layout.activity_main); is not doing what you think.
To naviguate to activities you have to use intents like the others sugested in the comments
First decalre your context:
final Context context = this;
then pass it to your intent:
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
}
});
The method launchAbout(null) is undefined for the type new View.OnClickListener(){}
package org.example.asteroides;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Asteroides extends Activity {
private Button bAbout;
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bAbout =(Button) findViewById(R.id.Button03);
bAbout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
launchAbout(null);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.asteroides, menu);
return true;
}
}
you can fix it like this:
bAbout.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
launchAbout(null);
}
private void launchAbout(Object object){
....
}
});
or define a method named launchAbout in the class Asteroides
If this is your complete code, it's pretty obvious to get that error because method launchAbout is not declared.
If this is not your complete code, please edit your answer.