I'm trying to get some sound in my app, but I can't get the mediaplayer working! I got some errors like:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
with the following code:
private MediaPlayer mPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
mPlayer = MediaPlayer.create(this, R.raw.NAMEOFSOUND);
}
protected void playBtnClicked(){
mPlayer.start();
}
It just doesn't work, whatever I do ... Does someone got some tips/Can someone help me out? The ''NAMEOFSOUND'' is a .mp3 file, I don't know if it even matters? Thank you!
SOLVED: I just solved it by coverting the file with a converter. Just changing the name+.mp3 didn't solve it; NullPointerException in Java Android App MediaPlayer , comment of Berty did the trick!
As you can see in the supported Media formats .mav is not supported by android. you should convert it to mp3 or any of the supported file formats
Related
I am starting EditCardActivity from MainActivity.
The app crashes first time and Android system shows an alert to "Open App Again".
When I reopen the app, it works as expected.
I have seen many answers on this site and I have done following according to those answers but it didn't work.
I have called the setContentView() inside onCreate() method before calling findViewByID().
I have verified that the ID I am passing in findViewByID() is spelled correctly.
I also tried to make the EditText a class member too and initialize it in onCreate() method.
I also tried using onStart(), onPostCreate() methods too.
I feel that the View has not been loaded when I am trying to call it and thus findViewByID() returns null. Hence I tried using Thread.sleep(1000) to give it 1 second to load but still same issue.
Here's the part of code that is having problem.
// MainActivity.java
public void editCard(View v) {
Intent i = new Intent(this, EditCardActivity.class);
startActivity(i);
}
<!--activity_edit_card.xml-->
...
<EditText
android:id="#+id/add_card_category_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin"
android:ems="10"
android:hint="#string/add_card_category_et_hint"
android:inputType="text"
android:textSize="#dimen/font_size" />
...
// EditCardActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_card);
showContents();
}
private void showContents() {
EditText editCardCategoryET = findViewById(R.id.add_card_category_et);
editCardCategoryET.setText(currentCard.getCategory()); // This line is throwing NullPointerException
}
Error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Edit:
I got what was causing the error.
I was referring to the add_card_category_et instead of edit_card_category_et.
The former view belonged to the different layout file of another Activity of my application.
I am sorry I couldn't catch this small error.
Although small, it made me stuck for 3 days.
Anyway thanks for your answers and comments.
Because editCardCategoryET cant find any value. So it throws Exception. It can be solve by add extra character by adding " " . Then app will not crush. editCardCategoryET.setText(" "+currentCard.getCategory()); .
Also you can handle it in a try(), catch() method.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
-edit 2. Tested this on my mobile works as intended. Stupid android avd
I am getting a error and i am not quite sure how to fix it. I could remove the need for caching the image but that would increase the bandwidth needed for the application.
My current code should work but it is throwing a NullPointerException
here is the error
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference
at android.content.ContextWrapper.getCacheDir(ContextWrapper.java:232)
at com.program.programmy.application.FileCache.<init>(FileCache.java:18)
and this is the code causing the problem is the one surrounded by ** line 18
public FileCache(Context context) {
// Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(
android.os.Environment.getExternalStorageDirectory(),
"ParseListViewImgTxt");
else
**cacheDir = context.getCacheDir();**
if (!cacheDir.exists())
cacheDir.mkdirs();
}
if someone could help out with this one little thing my program should work no problem. cheers
-Edit
This is only when i try to leave the list intent to look at a single object. Here is some more code from the area that prompts the error
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SingleItemView.class);
context.startActivity(intent);
}
});
Also here is a link to something similar that i am doing except no one is having the problem i am
Whenever your constructor is called, you are not sending any object in context.
Exception occurred as context object is null, and you are calling getCacheDir() method on it.
If you have problem in getting context object. Please read top voted answer in following link:
Static way to get 'Context' on Android?
I am trying to play an audio file using the code below. I have the audio fileset p and everything is goo except for where it says context.context comes up in red so how do i `fix that or what am I suppoesed to put in there to make the snippet of code work. Any and all help is appreciated and thanks in advance. BTW i am using android studio if that helps.
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
I figured it out all i have to replace context with is "this" so the code is like below. Leaving this up in case someone in the future needs it.
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sound_file_1);
mediaPlayer.start();
So I hope it's not a repeated question but, from the following code
File f = new File(Environment.getExternalStorageDirectory(), TRYVID);
Uri uri = Uri.fromFile(f);
mc = new MediaController(this);
mp.setMediaController(mc);
mp.setVideoPath("/sdcard/try2.mp4");
this is part of a function that's called when a button is pressed, what i'm hoping to achieve is that when the user presses a key, the video plays but i've learned that the videoview does not play anything from the raw folder so i copied the video into the sdcard, but then after i press on the button on the emulator, it just crashes says it has to be close unexpectedly. I tried both the .setVideoPath as well as the .setUri but both does not work hmm anyone can point to my problem here?
Ok so first off you need to make sure that you use the .setAnchorView(View v) on your mediaController or else it wont correctly control the videoView. Also your missing your .start() to actually start the video. Having recently done something similar with streaming from an rstp video file i can tell you there there is a chance its not working because your running it on an emulator, the video playback on AVD's often doesn't work. Try running it on a physical device if you have access to one, also read the logcat to get a better idea of where the errors are happening.
I hope this helps.
For playing video files an from SD card you can try this:
String filepath = Environment.getExternalStorageDirectory()+"/a.mp4";
VideoView vv = new VideoView(getApplicationContext());
setContentView(vv);
vv.setVideoPath(filepath);
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
Try this below code this wii surely solve your problem,
Make videoView,
VideoView videoView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
videoView = (VideoView)findViewById(R.id.VideoView);
videoView.setVideoPath("/sdcard/blonde_secretary.3gp");
videoView.start();
}
I wish it will help you.
I had the same question and found the solution. My code in the link works fine.
Check this question of mine
I have a soundboard uploaded on the android market. The app is doing pretty well in the market(50,000+ downloads), but the developer console reports that I have an error, and this is bothering me.
All crash reports come from only one device - Motorola Droid. I've looked at what the error actually is, and it happens when I call the start() method for the MediaPlayer class. I get the following:
java.lang.NullPointerException:
at com.meeg.soundit.Soundboard.playAudio(Soundboard.java:2517)
the code for the method playAudio is as follows and line 2517 is mp.start():
public void playAudio(int resid){
final MediaPlayer mp = MediaPlayer.create(this, resid);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mp.release();
}
});
}
Like I said, my soundboard has over 50,000 downloads, and it has 80 reports, all from the Motorola Droid. Is this something that I should ignore because 80 reports isn't much compared to how many people have used this, is there a problem with Moto Droid's and MediaPlayer, or is it just my code thats faulty?
This was a problem earlier on some builds that causes playback from resources to not work quite right. But you should fix your code to check for null and display appropriate message to the user.