I'm trying to make a quiz app and everything was working fine until I put in my code for my second button, now when I click start nothing happens and clicking study brings up a black screen. Start is supposed to take the user to a different activity, and study is supposed to take them to a website. Can someone check what's wrong with my code?
package com.example.rupin.whosthatpokemon;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class questionactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questionactivity);
Button start = findViewById(R.id.start);
start.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent (getApplicationContext(), one.class);
startActivity(intent);
}
});
start = findViewById(R.id.study);
start.setOnClickListener(
new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent i;
i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.pokemon.com/us/pokedex/"));
startActivity(i);
}
});
}
public void goToActivity2 (View view){
Intent intent = new Intent (this, one.class);
startActivity(intent);
}
}
As Loris Securo said in the comments, "
you have btn.setOnClickListener twice instead of start.setOnClickListener". That means you never set the onClickListener of your start button.
Also, in the second onClickListener, you have:
i = new Intent(view.getContext(), one.class);
You should instead do:
i = new Intent(questionactivity.this, one.class);
Although view.getContext() should technically work, I always seen this used as the first parameter in the Intent constructor which is a Context object. Since this (an instance of an Activity) can be cast to a Context, it is better to get the context of the outer class , and this would explain why you get a black screen when trying to go to the other activity.
Side note: Your class names should start with a capital letter and be CamelCased such as ClassOne or QuestionActivity.
Related
I'm trying to develop a game app. At the gameover screen, I want to have a button that goes back to the start when you click it. But the problem is, it doesn't work. I really don't know why it doesn't work, I have tried everything but can't find the problem. Can someone help me?
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class GameOver extends AppCompatActivity {
MediaPlayer gameoversound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
Button weiter_button = (Button) findViewById(R.id.weiter);
weiter_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { goToMainActivity(); }
});
}
private void goToMainActivity() {
Intent back = new Intent( this, MainActivity.class);
startActivity(back);
}
}
Change this to GameOver.this
You don't have to cast widgets explicitly anymore, except for some special cases. It has been this way for a while.
If the game is over, you'd best insert a finish(); after launching the main Activity. You don't want users to be able to go back to the gameover screen by pressing the back button.
Set up FLAGS for your Intents. This comes in handy because if you didn't finish(); some Activity it remains in the stack, so it will be launching that one, but then you might want a new one. This will cause issues in navigation.
Add/ check your onBackPressed() methods for the 2 Activities.
Furthermore, specify that you are overriding the method
#Override
public void onClick(View view)
{
}
In the XML, <Button> tag, add
android:clickable=true
android:focusable=true
I'm having trouble launching an instance of a class with an intent.
My app has an initial screen with three textviews 'Cakes, Drinks, and Biscuits'. An onClick listener is set on these textviews, such that when the user clicks on 'Cakes' it goes to a list of cakes (Chocolate, Lemon, Carrot), and the user clicks on 'Drinks' it goes to a list of drinks (Tea, Coffee, Coke), and the same for Biscuits. Currently, I have an intent that launches a Cakes class, and within that class there is an arrayList that has strings (Chocolate, Lemon, Carrot etc.), when the user clicks on Drinks it launches a separate class called Drinks and does the same, ditto with Biscuits. However, I gather it is better coding practice to have simply one class for these three, and have separate instances of that class (with different arrayLists to separately list the cake, drinks and biscuits).
I assume I should make an 'items' class (i.e. the arrayList with Chocolate, Lemon, Carrot, and all the drinks, and all the biscuits) but don't know how to launch a specific instance of the 'items' class with my intent written in the mainActivity. Any help appreciated!
The mainActivity.java where the initial three items are listed:
package com.example.android.recipe;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView history = (TextView) findViewById(R.id.cakes);
history.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent cakesList = new Intent(MainActivity.this, cakeActivity.class);
startActivity(cakesList);
}
});
TextView examination = (TextView) findViewById(R.id.drinks);
examination.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent drinksIntent = new Intent(MainActivity.this, drinksActivity.class);
startActivity(drinksIntent);
}
});
TextView other = (TextView) findViewById(R.id.biscuits);
other.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent biscuitsIntent = new Intent(MainActivity.this, BiscuitsActivity.class);
startActivity(biscuitsIntent);
}
});
}
}
This is the 'CakeActivity.java' which gets launched when you click on 'Cakes', I would like to turn this into a generic class which gets launched when you click on Cakes, Coffee, or Biscuits but it comes up with the relevant ArrayList. NB I have a custom ArrayAdapter called 'Record' because, when I further develop this app, clicking on Chocolate, Lemon, etc. will have a list of separate ingredients required to make that Cake (these will also in the future not be separate classes).
package com.example.android.recipe;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class cakeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record_list);
final ArrayList<Record> records = new ArrayList<Record>();
records.add(new Record("Chocolate", ChocolateCake.class));
records.add(new Record("Lemon", LemonCake.class));
records.add(new Record("Ginger", GingerCake.class));
RecordAdapter adapter =
new RecordAdapter(this, records, R.color.colorTan);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Record record = records.get(position);
Intent intent = new Intent(getApplicationContext(), record.getListedItemId());
startActivity(intent);
}
});
}
}
NB this is exemplary code, and not my actual app - the reason I mention this is because I am going to have a really long list that, in the way in which my code is currently written, would require >100 classes if I continued in the way I am above.
Yyou can use intent extra value:
The easiest way to do this would be to pass the type to the activity in the intent.
Intent intent = new Intent(getBaseContext(), AllInOneActivity.class);
intent.putExtra("type", "cake");
startActivity(intent);
Access that intent on next activity
String type = getIntent().getStringExtra("type");
Load your data according to type:
final ArrayList<Record> records = new ArrayList<Record>();
if(type.equals("cake"))
{
records.add(new Record("Chocolate", ChocolateCake.class));
records.add(new Record("Lemon", LemonCake.class));
records.add(new Record("Ginger", GingerCake.class));
}
else if(type.equals("Drinks"))
{
records.add(new Record("fruty", whatever.class));
}
Try this and let me know. if problem persist.
This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 6 years ago.
So far from all the tutorials I've looked at, most only get to the point of "Button Was Clicked" I need my second activity button to open a new activity.
I named this class, fifth_layout.xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Amazon"
android:drawableLeft="#drawable/amazon"
android:drawableStart="#drawable/amazon"
android:layout_weight="0.07"
tools:ignore="HardcodedText"
android:id="#+id/button10"
android:textSize="35sp" />
After that in my FifthActivity.java I have
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
});
}
}
I just need the button to be able to open a new blank activity. But when i click the button nothing happens? I just need a new activity. i feel like the code is correct i just need help on what i might be doing wrong.
You have to use intent to open a new Activity. Assuming you want to open an activity called SixthActivity from your FifthActivity.
You should use this:
public class FifthActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifth_layout);
Button button = (Button) findViewById(R.id.button10);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(FifthActivity.this,SixthActivity.java);
FifthActivity.this.startActivity(intent);
}
});
}
}
Hope this helps,
Regards.
Your onClickListener does nothing, of course nothing happens.
Create a new Activity (let's say you name it NewActivity, add it to the AndroidManifest.xml and add the following code you your existing activity:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final Intent intent = new Intent(FifthActivity.this, NewActivity.class);
startActivity(intent);
}
});
I have a very strong feeling you're kind of lost in Android Development. I strongly suggest you follow Udacity's Android Development course.
Alright, so you have the single activity with its layout, right?
What your asking is "how do I launch another activity with another layout?"
To do this, we'll use an "intent" (think of an intent as how the activities talk to eachother, they get passed back and forth)
To create the intent and start, you'll need these couple lines:
Intent intent = new Intent(this, Target.class);
startActivity(intent);
Which should work within your onClick.
If you created the activity within Android Studio with File>New>Activity, this should have put the activity in your AndroidManifest.xml already, otherwise you'll need to add it yourself.
I have managed to successfully start two activities using the ImageButton along with .setOnClickListener tied to it, i have also included layouts with different ImageButtons. Each button launches an activity. I have created the activities. I have also managed to remover crash bugs, lint errors, have the latest Android SDK. However now the buttons stop working even though you hear the click. Neither the activity launches on the first imageButton nor the second one.
This happens the moment i put multiple ImageButtons in. It works with 1 button. I suspect the (this) command in the class is confused as to what to call. My intent call method to start new activities are streamlined and basic for quick, uncomplicated access.
Can someone please help me as to why the multiple setOnclickListener cannot be tied to the relevant imageButtons please?
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends ActionBarActivity implements OnClickListener {
ImageButton imageButton1;
ImageButton imageButton2;
ImageButton imageButton3;
ImageButton imageButton4;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reusable_layout);
imageButton1=(ImageButton)findViewById(R.id.imageButton1);
imageButton2=(ImageButton)findViewById(R.id.imageButton2);
imageButton3=(ImageButton)findViewById(R.id.imageButton3);
imageButton4=(ImageButton)findViewById(R.id.imageButton4);
imageButton1.setOnClickListener(this);
imageButton2.setOnClickListener(this);
imageButton3.setOnClickListener(this);
imageButton4.setOnClickListener(this);
}
public void onClick1(View view) {
Intent intent =
new Intent(this, OtherActivity.class);
startActivity(intent);
}
public void onClick2(View view) {
Intent intent =
new Intent(this, OtherActivity2.class);
startActivity(intent);
}
If you notice I have only coded 2 of the four buttons (so once this code works in theory the others should).
This is what OtherActivity from imageButton1 calls.
package com.example.startanotheractivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class OtherActivity extends Activity
implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_layout);
Intent intent = getIntent();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
and this is the activity imageButton2 calls
package com.example.startanotheractivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class OtherActivity2 extends Activity
implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_layout_2);
Intent intent = getIntent();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Here is my layout.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="566dp"
android:layout_height="456dp"
android:background="#null"
android:src="#drawable/gatanga1"
android:onClick="onClick1" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="540dp"
android:layout_height="189dp"
android:background="#null"
android:src="#drawable/gatanga2"
android:onClick="onClick2" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="540dp"
android:layout_height="189dp"
android:background="#null"
android:src="#drawable/gatanga3"
android:onClick="onClick3" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="540dp"
android:layout_height="189dp"
android:background="#null"
android:src="#drawable/gatanga4"
android:onClick="onClick4" />
</LinearLayout>
Thank you
Many many thanks.
use a switch statement in onClick method
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.imageButton1: /** Start a new Activity OtherActivity.java */
Intent intent = new Intent(MainActivity.this, MyCards.class);
this.startActivity(intent);
break;
case R.id.imageButton2: /** Start a new Activity OtherActivity2.java */
Intent intent2 = new Intent(MainActivity.this, OtherActivity2.class);
this.startActivity(intent2);
break;
}
The onClick method passes the clicked view as an variable, you can use a switch statement to determine which view was clicked and then you can perform your operations..
public void onClick(View v) {
switch(v.getId()){
case R.id.imageButton1:
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
break;
case R.id.imageButton2:
Intent intent = new Intent(MainActivity.this, OtherActivity2.class);
startActivity(intent2);
break;
}
}
Scholars, Educated Aristocrats and of course Plumbers.
Thank you for replying. It works now by the code i did, although i will try the above solutions. I have experimented with switches and i do like them. I want to make sure memory management and file sizes are kept to a minimum as the end app will have about 40 activities.
However the way i did it was i removed all the other ImageButtons declarations in MainActivity except:
public class MainActivity extends ActionBarActivity implements OnClickListener {
ImageButton imageButton1;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reusable_layout);
imageButton1=(ImageButton)findViewById(R.id.imageButton1);
}
The onClick methods then were left as onClick1,onClick2. Both activities worked. I added onClick3 and onClick4 like the below:
public void onClick1(View view) {
Intent intent =
new Intent(this, OtherActivity.class);
startActivity(intent);
}
public void onClick2(View view) {
Intent intent =
new Intent(this, OtherActivity2.class);
startActivity(intent);
}
public void onClick3(View view) {
Intent intent =
new Intent(this, OtherActivity3.class);
startActivity(intent);
}
public void onClick4(View view) {
Intent intent =
new Intent(this, OtherActivity4.class);
startActivity(intent);
}
I think what seemed to happen then is the code wasnt confused or stuck in a loop (it didnt crash on my original posting). But instead now it goes straight to layout, gets the "onClick1" for button 1 and "onClick2" and matches them with the public void onClick(X) in MainActivity, because i have just declared i am using the ImageButton and an Intent, right after onClickListener, it works.
Note if i move around this code it breaks or will give me the same error. Regardless, i am happy it works but am intrigued as to why i did not have to declare all my buttons. Maybe it is just the logic in Java Code working.
I will definately try the switches and amendments later experimenting.
With best wishes.
in the MainActivity you should correctly implement OnClickListener interface:
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.imageButton1:
Intent intent =
new Intent(this, OtherActivity.class);
startActivity(intent);
break;
case R.id.imageButton2:
Intent intent =
new Intent(this, OtherActivity2.class);
startActivity(intent);
break;
}
}
I'm trying to start a new activity from a non-activity class.
From the main menu:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Menu extends Activity {
Button start, options;
GameLoop Game = new GameLoop();
#Override
public void onCreate(Bundle mainStart) {
super.onCreate(mainStart);
setContentView(R.layout.menu);
start = (Button) findViewById(R.id.bStart);
options = (Button) findViewById(R.id.bOptions);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openStart = new Intent(Menu.this, Game.class);
startActivity(openStart);
}
});
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context mContext = null; //Error called for mContext to be initialized so just tried setting to null. This is most likely the error cause it would make more sense for it to be equal to "getContext()" or something like that
Game.Start(mContext);//Here
}
});
}
}
I'm trying to open an activity from the Game.Start() method.
import android.content.Context;
import android.content.Intent;
public class GameLoop extends Menu{
boolean hello = false;
public void Start(Context sContext){
Intent openOptions = new Intent(sContext, Options.class);
startActivity(openOptions);
}
}
I'm not sure if using context would be the right way of going about this but I figured it was worth a try. Im entirely new to java and android so I'm pretty much lost on where to go next. Any help in what direction to take would be throughly appreciated.
Activity extends Context, so you can just use this when inside Activity.
Game.Start(Menu.this);
I use Menu.this because you are inside inner anonymous class (View.OnClickListener) where this refers to this inner class.
Do you added the new activities to the androidmanifest.xml?