Application crash on android - java

I don't know the reason of the crash.
package com.tct.soundTouch;
//imports ();;;;;;;
public class Main extends Activity implements OnClickListener{
private MediaPlayer mp;
private MotionEvent event;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
final ImageButton zero = (ImageButton) this.findViewById(R.id.button);
zero.setOnClickListener(this);
mp = MediaPlayer.create(this, R.raw.sound);
}
public void onClick(View v) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
}
the log
thanks

I think the problem is in the line switch (event.getAction()) {. Where did you initialize event? I think this causes the NullPointerException.
Btw... You shouldn't name your class main. Use Main at least.

I'm not seeing event being set to a non-null value in the code you posted. Unfortunately, there is no "up" or "down" to a click event received via an OnClickListener.
If you're looking for a toggle-like effect, you might use MediaPlayer#isPlaying():
public void onClick(View v) {
if (mp.isPlaying()) {
mp.pause();
} else {
mp.setLooping(true);
mp.start();
}
}
If you need to handle MotionEvent.UP and MotionEvent.DOWN then you should implement View.OnTouchListener:
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
return true;
case MotionEvent.ACTION_UP:
mp.pause();
return true;
}
return false;
}
and then set it using setOnTouchListener:
zero.setOnTouchListener(this);

Related

Whe button continueButton isnt working? It should start the new Activity if I have a text in EditText or make a Toast

I work with a program which should make parameters for character. A person writes a name then generate force and health by clicking the button. After all, he click the continueButton which should start the MainActivity if name exists or make a Toast text. But it's not working. The problem should be in the end of onClick method.
There is a code
public class CreateActivity extends AppCompatActivity
implements View.OnClickListener {
final Random random = new Random();
String toastText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
}
#Override
public void onClick(View v) {
Button continueButton = findViewById(R.id.continueButton);
Button getData = findViewById(R.id.getData);
final EditText newName = findViewById(R.id.newName);
TextView newHealth = findViewById(R.id.newHealth);
TextView newForce = findViewById(R.id.newForce);
continueButton.setOnClickListener(this);
getData.setOnClickListener(this);
newName.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()== KeyEvent.ACTION_DOWN &&
(keyCode == KeyEvent.KEYCODE_ENTER)){
Character.name = newName.getText().toString();
return true;
}
return false;
}
});
switch (v.getId()){
case R.id.getData: {
Character.health = random.nextInt(100);
newHealth.setText(String.valueOf(Character.health));
Character.force = random.nextInt(100);
newForce.setText(String.valueOf(Character.force));
}
// THERE IS A PROBLEM
if (newName.getText().toString().equals("")) {
switch (v.getId()) {
case R.id.continueButton:
startActivity(new Intent(this, MainActivity.class));
finish();
return;
}
} else{
switch (v.getId()) {
case R.id.continueButton:
Toast toast = Toast.makeText(this, toastText, Toast.LENGTH_SHORT);
toast.show();
}
}
}
}
}
Initialize your Button inside onCreate method. Like this:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener {
private Button yourButton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourButton= findViewById(R.id.your_button_id);
yourButton.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
switch (view.getId()) {
case R.id.your_button_id:
// Do something
}
}
}

How to play audio only while button is held down?

I am trying to make a simple app: there is one button in the middle which a child would press. As long as that button is held down it would play a certain MP3.
At the moment, I tried with onClick, but, that plays only when the button is released.
Instead of using an onClickListener which exposes nothing more then an interface for press and release, you would need to use an onTouchListener - which exposes all touch events of a view.
myButton.setOnTouchListener( new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
MotionEvent.ACTION_DOWN:
// start playing
return true;
MotionEvent.ACTION_UP:
// stop playing
return true;
}
return false;
}
});
Try something like this. Using the boolean "playing" you can create a thread/loop elsewhere to check if still playing and keep audio going.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN) {
playing = true;
} else {
playing = false;
}
return true;
}
});
Try the OnFocusChangeListener
Button.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// code to execute when Button loses focus, i.e. stop music
}
}
});
Since a button is a view component you can use View.OnTouchListener event listener with ACTION_BUTTON_PRESS & ACTION_BUTTON_RELEASE MotionEvent.
Here is an example:
yourButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_BUTTON_PRESS){
// Start Video
return true;
} else if (event.getAction() == MotionEvent.ACTION_BUTTON_RELEASE) {
// End Video
return true;
}
return false;
}
});
This is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start_recording(View view){
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.test1);
mediaPlayer.start();
}
}

How to set on touch listener for multiple image views?

I created an onTouchListener for my ImageView "car" and want to do the same for another ImageView, however I can't figure out how. So my question is:
How do you use one onTouchListener that detects the MotionEvents of two separate ImageViews and makes something happen accordingly?
#Override
public boolean onTouch(View view, MotionEvent event) {
//int action = event.getAction();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
carcolor.setBackgroundColor(this.getResources().getColor(R.color.colorPrimary));
car.startAnimation(pressdown);
pressdown.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
carback.setScaleX((float) 0.9);
carback.setScaleY((float) 0.9);
}
#Override
public void onAnimationEnd(Animation animation) {
carback.setVisibility(View.VISIBLE);
car.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
car.startAnimation(release);
carcolor.setBackgroundColor(this.getResources().getColor(R.color.unpressed));
release.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
carback.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationEnd(Animation animation) {
car.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
You should implement the onTouchListener like:
imageView.setOnTouchListener(this);
and init it like :
#Override
public boolean onTouch(View v, MotionEvent event) {
ImageView view = (ImageView) v;
switch (view.getId()){
case R.id.car1: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
case R.id.car2: // example id
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_CANCEL:
break;
}
break;
}
return true;
}
If your activity is implementing the onTouchListener event (I'm assuming based on the override), on your other ImageView just do the following
imageView.setOnTouchListener(this);
Where this is the current activity.
EDIT: Based on your comment
public class ImageView1Listener implements OnTouchListener
{
**** OVERRIDES****
}
public class ImageView2Listener implements OnTouchListener{
**** OVERRIDS FOR THIS IMAGE****
}
then in your main remove the implements OnTouchListener and use bind it programatically
imageView1.setOnClickListener(new ImageViewListener1());
imageView2.setOnClickListener(new ImageViewListener2());
Attach the listener to the parent of them.
Than, use gesturesDetector as stated here:
https://developer.android.com/training/gestures/detector.html
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector;
// Called when the activity is first created.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
....
}

How to separate Button Longpress Button press Up and Down events

I have a Click function and long press on the same button. Implemented the long press event but, I need to find the button UP_EVENT and DOWN_EVENTS separately.
How can I implement by using the OnLongClickListener
View.OnLongClickListener listener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
return true;
}
};
Implement a TouchListener within the onLongClickListener:
View.OnLongClickListener listener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// PRESSED
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
// RELEASED
return true; // if you want to handle the touch event
}
return false;
}
});
return true;
}
};
To detect ACTION_UP and ACTION_DOWN events you need to implement OnTouchListener.
to sepate , you can do this way
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (isOnClick) {
//TODO onClick code
}
break;
case MotionEvent.ACTION_MOVE:
}
break;
default:
break;
}
return true;
}

Audio player with listview

i have audio files when i'm play another file then previous file play continuously.
so if you have solution please let me know.
here is my main.java
public class MusicAndroidActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonPlay = (Button) findViewById(R.id.play);
music = (ListView)findViewById(R.id.music);
// music.setAdapter(new ArrayAdapter<String>(this,, str));
music.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
switch (position){
case 0:
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.hosannatamil);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
case 1:
MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(),R.raw.one_less);
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
case 2:
MediaPlayer mp2 = MediaPlayer.create(getApplicationContext(),R.raw.words);
mp2.start();
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
break;
}
}
});
}
You need to manually call MediaPlayer.stop() if you want to stop the playback.
If you want to start the same MediaPlayer again you have to call .prepare() before that.
For more information read the reference page and look at the state diagram there.
if(mediaPlayer.isPlaying())
{
//stop or pause your media player mediaPlayer.stop(); or mediaPlayer.pause();
mediaPlayer.pause();
}
else
{
mediaPlayer.start();
}
Put this Condition inside switch statements before mediaPlayer.start() also create a global reference of Media Player instead of creating three references like MediaPlayer mediaPlayer instead of MediaPlayer mp,mp1,mp2 etc,because you can create media Player object in the same reference as one condition must be true all the time,also put a default condition in the switch statement!

Categories