Images, Buttons, Sounds and Share on Android

0

Hello, how are you? I tell you I'm a Java student (I'm just starting) and I wanted to try doing an application on Android by following some YouTube tutorials, I managed to do it and now I want to improve it, I find some problems that I'll detail:

1) I wanted to improve the buttons, for this I put an ImageButton to which I put an image that is in my res > dawable and it's jpg background has the gray button which when I go to "Background" and I charge ic_launcher_foregraund I see a compilation error in the top right which says "Could not resolve resource @ drawable / ic_launcher_foreground", in the drawable folder I have an xml that is called "ic_launcher_foreground.xml (v24)" when I open this file it does not appear no error in the code (I mean nothing in red), maybe I need to write something, I have already given Clean Project, Rebuild Project, I went to File > Invalidate Caches / Restart and follow the problem, with other buttons that do not use image it does not happen to me.

2) The application when I want to try it with the cell phone opens and closes alone with a sign that says "the application has stopped working" or something like that, I want to clarify that Android Studio does not mark errors in any file, The error I commented on in the previous question about "Could not resolve resource @ drawable / ic_launcher_foreground" goes away when I delete what it says in Background on the button, therefore, there is no error neither in the MainActivity nor in the Text part. only one that comes out is down when I display the run button that appears this:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.lucas.botoneradeteloresumoasinomas, PID: 14619
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lucas.botoneradeteloresumoasinomas/com.example.lucas.botoneradeteloresumoasinomas.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3320)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
    at android.app.ActivityThread.access$1100(ActivityThread.java:230)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7409)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
    at com.example.lucas.botoneradeteloresumoasinomas.MainActivity.onCreate(MainActivity.java:40)
    at android.app.Activity.performCreate(Activity.java:6904)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:230) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:7409) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

where it says caused by I have it declared in the MainActivity as:

Button peroperoperopero;
peroperoperopero = (Button) findViewById(R.id.peroperoperopero);
peroperoperopero.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mp = MediaPlayer.create(MainActivity.this,R.raw.peroperopero);
            mp.start();
        }
    }); 

that's why the error is there because in one part of the build error it says this:

Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.Button
    at com.example.lucas.botoneradeteloresumoasinomas.MainActivity.onCreate(MainActivity.java:40)

but it does not appear in red nor does the MainActivity mark me wrong.

3) I do not know what code to write so that when a button is playing a sound and another button is pressed, this first button that was playing a sound stops and the other button starts to play, currently if I precede two buttons are reproduced both and as long sounds are added and nothing is understood, I would also like to know what code to write to press a button that reproduces the sound and when I return to play that button while it is playing this stop I currently have this code:

peroperoperopero.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mp = MediaPlayer.create(MainActivity.this,R.raw.peroperopero);
            mp.start();
        }
    }); 

4) Finally I would like to know what code to write to put a share button on WhatsApp and social networks the sounds of this keypad.

Sorry for the length of the question, I'm quite new in this and there are many things that escape me, let me know if you do not understand something or if I have to post some code that is missing to understand any of the questions what I did.

    
asked by Lucas Fajersztejn 03.10.2018 в 00:02
source

1 answer

0

1) "Could not resolve resource @ drawable / ic_launcher_foreground", in the drawable folder I have an xml that is called "ic_launcher_foreground.xml (v24)". In this case the file ic_launcher_foreground.xml must be found in the directory /drawable and not only in /drawable-v24 , check this situation.

2) "Could not resolve resource @ drawable / ic_launcher_foreground", this is solved with what I indicated in point 1.

Also: I think that according to your error message,

  

Caused by: java.lang.ClassCastException:   android.support.v7.widget.AppCompatImageButton can not be cast to   android.widget.Button       at com.example.lucas.botoneradeteloresumoasinomas.MainActivity.onCreate (MainActivity.java:40)

the button with id peroperoperopero is a ImageButton and not a Button , performs the appropriate casting:

//Button peroperoperopero;
//peroperoperopero = (Button) findViewById(R.id.peroperoperopero);
ImageButton peroperoperopero;
peroperoperopero = (ImageButton) findViewById(R.id.peroperoperopero);

3) You should check if a sound is playing, if so you must stop and release the resource to start a new reproduction, if it is not playing simply start the MediaPlayer as you are doing.

  if(mp.isPlaying()){ //Play a new sound
        mp.stop();
        mp.release();
        mp = null;
        sound = MediaPlayer.create(this,x);
  }else{ //Play sound
        mp = MediaPlayer.create(MainActivity.this,R.raw.peroperopero);
        mp.start();
   }

4) review:

Add option to share a .Mp3 file in networks by default in the Android app

    
answered by 03.10.2018 в 00:15