I'm a beginner and have developed a simple app which plays a sound when you click some button. The app runs just fine, but when i click it, there´s no sound.
Olá! Fiz um app que simplesmente reproduz um som ao clicar em um botão.
O app emula normalmente (abre no emulador), mas quando clico no botão para reproduzir um som, ele não funciona.
MainActivity.java
package mucap.esy.es.preplay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class BasicScreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.justdoit);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});
}
}
}
I have everything set (file, button) but it won´t work with this code.
I found nothing in logcat, can someone help me?
please Update your MainActivity like this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button)this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.justdoit);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});
}}
Related
I am learning Android and I have build a simple Android app that shows Toasts for starters.
In the main activity I have a button that when I press it it shows a Toast and another button that takes you to another activity. This is the code:
package ro.serbab.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.addnotexml);
}
});
Button button2 = (Button) findViewById(R.id.button5);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Serban Android",
Toast.LENGTH_SHORT).show();
}
});
}
}
This is the second activity:
package ro.serbab.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ro.serbab.notes.R;
public class addnote extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnotexml);
Button button3 = (Button) findViewById(R.id.button2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Serban
Android",Toast.LENGTH_SHORT).show();
}
});
}
}
The button from the second activity doesn't show any toast. I keep pressing it and it doesn't work.
remove setContentView(R.layout.addnotexml); this line from onClick and add
when you want to go 2nd activity onClick the button this code will help you out
Intent intent = new Intent(MainActivity.this, 2ndActivity.class);
startActivity(intent);
i get some problem when i use the intent, he said "cannot resolve constructor Intent('....')" , i dont know what is wrong in my code but you can see what i m importing here
package com.example.noen.myintentapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn_move);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.btn_move:
Intent intn = new Intent(MainActivity.this,target1.class);
break;
}
}
}
From the main page, i click the image and it will open the question 1. Now, I trying to open a new activity which is question 2 using image view from the question 1. But it has an error: question 1 is not an enclosing class.
Here is the code for Main Activity.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button video to video page
b = (ImageView) findViewById(R.id.btnPlayVideo);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, VideoPage.class);
startActivity(i);
}
});
//button start test to question1
b = (ImageView) findViewById(R.id.btnStartTest);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Question1.class);
startActivity(i);
}
});
//button question 1 ke question 2
b = (ImageView) findViewById(R.id.btn1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
}
});}}
This is question 1.java. it said that it is not an enclosing class.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
}
This is question 2.java
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
}
}
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
You are trying to start Question2 from the context of Question1 within MainActivity. The first parameter of new Intent() should be the context of the enclosing class that will start the next activity. Since you are defining this in MainActivity, the context should be MainActivity.this. There is a great introduction to this topic here.
Either change the first parameter of new Intent() to "MainActivity.this" or move the entire second OnClickListener to a view that is within Question1.
First of all, please don't mark it a duplicate. I found similar question on stackoverflow.com but I could not find my solution. Please look at this image
Screenshot
I am trying to check that EditText should not be empty. But the setOnClickListener is displayed in red. I tried solutions suggested in other similar questions and mentioned import android.view.View.OnClickListener; but still it is not working.
So, any help that could help me solve the problem from anyone is really appreciated.
This is the java file
package com.example.rtrjs.abc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
}
EditText edittext=(EditText)findViewById(R.id.etfn);
Button buton=(Button)findViewById(R.id.breg);
buton.setOnClickListener(new OnClickListener())
}
When it showed error I didnot proceed further.
First you are not inside any method, so you will not be able to assign the listener, second you are missing the onClick method inside de OnClickListener. Try this:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//do your stuff
}
});
}
Because you're not in the onCreate() method
EDIT :
Try this way :
package com.example.rtrjs.abc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
EditText edittext=(EditText)findViewById(R.id.etfn);
Button button=(Button)findViewById(R.id.breg);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//process
}
});
}
}
Also I suggest you to use the string.xml file for your "Sign up" in order to be prepared for multi language support ;)
You should write the OnClickListener function code for your Button and EditText in the OnCreate method like this
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
EditText edittext=(EditText)findViewById(R.id.etfn);
Button button=(Button)findViewById(R.id.breg);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//process
}
});
}
}
Update your code to this:
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
getSupportActionBar().setTitle("Sign up");
EditText edittext=(EditText)findViewById(R.id.etfn);
Button button=(Button)findViewById(R.id.breg);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
// logic here
}
});
}
}
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();
}
});