Android: Why isn't my button playing its sound effect? - java

The first button works fine, but when I click to the second, nothing happens. I'm not getting any errors, I can't see what's wrong.
Sounds.java
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
}

You can't randomly create a method like you're doing with onCreate1, it's never executed by you or by your Activity.
This is all you need:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(this);
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}

You aren't assigning the onclick listener inside the actual OnCreate. This should work:
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}

Related

Android Webview doesn't load the Url from button listener

It loads the site but only when it's not in the Action Listener.
Even if I load the url way down in the onCreate method it works.
I already checked if the Webview is invisible, the button listener works also.
The problem began after a few hours of coding other things
WebView wbvCheckURL;
Button btnSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wbvCheckURL = findViewById(R.id.wbvCheckURL);
btnSearch = findViewById(R.id.btnSearch);
wbvCheckURL.setWebChromeClient(new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
...
}
#Override
public void onReceivedTitle(WebView view, String title) {
...
}
});
wbvCheckURL.getSettings().setJavaScriptEnabled(true);
wbvCheckURL.setWebViewClient(new WebViewClient());
// When i do it like this it works:
// wbvCheckURL.loadUrl("http://www.google.de");
btnSearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
wbvCheckURL.loadUrl("http://www.google.de");
hideKeyboard(v);
LoadCode();
}
});
}
The Solution is to load it in a Thread
Create instance and start the thread:
LoadURL t = new LoadURL();
t.start();
Thread class:
class LoadURL extends Thread {
public void run() {
Classname.wbvCheckURL.post(new Runnable() {
public void run() {
Classname.wbvCheckURL.loadUrl("http://www.google.de");
}
});
}
you can try mine. Mine is another kind of webview with Drawer Layout. Hope you get the idea on how to insert the webview.
package com.example.appname;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuItemCompat;
public class Provokinc extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provokinc);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://stackoverflow.com/questions/63438455/android-webview-doesnt-load-the-url-from-button-listener");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
ImageButton ImageButton = (ImageButton) findViewById(R.id.imageButtonA);
ImageButton ImageButton1 = (ImageButton) findViewById(R.id.imageButtonB);
ImageButton ImageButton2 = (ImageButton) findViewById(R.id.imageButtonC);
ImageButton ImageButton3 = (ImageButton) findViewById(R.id.imageButtonD);
ImageButton ImageButton4 = (ImageButton) findViewById(R.id.imageButtonE);
ImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int1 = new Intent(Provokinc.this,Main3Activity.class);
startActivity(int1);
}
});
ImageButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int2 = new Intent(Provokinc.this, Main2Activity.class);
startActivity(int2);
}
});
ImageButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int3 = new Intent(Provokinc.this, Provokinc.class);
startActivity(int3);
}
});
ImageButton3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int4 = new Intent(Provokinc.this, ActivityItemDetails.class);
startActivity(int4);
}
});
ImageButton4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent int5 = new Intent(Provokinc.this, MainActivity.class);
startActivity(int5);
}
});
}
#Override
public void onBackPressed(){
if(webView.canGoBack()){
webView.goBack();
} else{
super.onBackPressed();
}
}
}

Error: setOnClickListener cannot be resolved

I encountered an issue and couldn't resolve in Android Studio. The setOnClickListener remains red and doesn't work unless I get rid of my "loseStarter1" button name.
Note: Starter1 is a button, I'm trying to make it disappear when clicked by the user. My real code starts when I introduce the loseStarter1 button.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
}
Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
})
}
Much appreciated.
You're missing a semicolon to end the new View.OnClickListener() { ... statement as well as that block not being inside of a method.
Not only move this code into the onCreate method, make sure you end it with a semicolon.
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); // Add the semicolon here
It should look like this:
public class game1 extends AppCompatActivity {
Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); //added semicolon
} // ends onCreate method
} // ends class
Your Button variable declaration and OnClickListener initialization is outside of the onCreate() method. Use the following code:
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
}
}
Move below code inside onCreate()
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
This is how i do it.
1) first make the class implement the interface View.OnClickListener, this let you handle the button clic event:
public class game1 extends AppCompatActivity implements View.OnClickListener;
2) second create the interface method to handle event.
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
3) OnCreate methods is the best way to find objects and set properties.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1= (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
all code :
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity implements View.OnClickListener {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
}
Advice:
Rename class to Game1.

Android Image Button

Android app, i wrote to test image button doesn't work. I have created a image button and implement an event listener for that button. What's wrong with this source code?
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.Toast;
public class ImageButtonTestApp extends Activity {
ImageButton imageButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void eventListenerOnButton() {
imageButton = (ImageButton) findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
u have written setOnClickListener in different method but u didnot call that method any where call that method in oncreate.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eventListenerOnButton();
}
try this one,
Try it
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(ImageButtonTestApp.this, "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});

Button one should aim to page one

I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!
ActiveMain.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTwo.class);
// you made a mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
}
}
PageTwo
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageTwo extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weoff);
}
}
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTWO.class); // you made a
mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});

button can not be resolved

This is my starting activity. I'm trying to get this button to work but it's been giving me this error.
Line 15 button can not be resolved.
package com.synamegames.giveaway;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GiveawayActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button register = (Button) findViewById(R.id.register);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
setContentView(R.layout.main);
}
}
Please try this..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
}
The problem is You have defined the Button instances as register in the line final Button register = (Button) findViewById(R.id.register); But you are setting onclick listener to the button instance which is not defined. You should have
register.setOnClickListener(new OnClickListener() {
instead of
button.setOnClickListener(new OnClickListener() {
U can use android:onClick from xml and pass a the view into the .java file
eg:
android:onClick="bactToList"
in java:
public void bactToList(View view){
}

Categories