Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:17:08+00:00 2026-06-16T01:17:08+00:00

I am a beginner, I’m trying to make a very simple app, one button

  • 0

I am a beginner, I’m trying to make a very simple app, one button and 4 .ogg files in the assets folder, that button would play random file from the above mentioned.

I have only one main class and one layout.

Here is what I did, the code doesn’t show any error but the app crash immediately when launching on emulator.

My main class:-

    MediaPlayer player;
AssetFileDescriptor descriptor;
private String[] filelist;

@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);

player = new MediaPlayer();

Button playSound = (Button)findViewById(R.id.button1);

AssetManager aMan = this.getAssets();
try 
{
 filelist = aMan.list("/assets");
} catch (IOException e1) {
    e1.printStackTrace();
}

playSound.setOnClickListener(new View.OnClickListener() 
{           
    public void onClick(View v) 
    {
        Random randomGenerator = new Random();
        int randomInt = randomGenerator.nextInt(filelist.length);

        try 
        {
            String mediaFile = filelist[randomInt];
            descriptor = getAssets().openFd(mediaFile);
                player.setDataSource(descriptor.getFileDescriptor(),descriptor.getStartOffset(),descriptor.getLength());
            descriptor.close();
            player.prepare();
                        player.start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}

My layout:-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/background"
    android:contentDescription="@string/desc" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="154dp"
    android:layout_marginRight="92dp"
    android:text="@string/button1" />

</RelativeLayout>

Here is the logcat:-

12-14 19:36:56.062: D/AndroidRuntime(540): Shutting down VM
12-14 19:36:56.062: W/dalvikvm(540): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 19:36:56.072: E/AndroidRuntime(540): FATAL EXCEPTION: main
12-14 19:36:56.072: E/AndroidRuntime(540): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.os.Looper.loop(Looper.java:137)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 19:36:56.072: E/AndroidRuntime(540):  at java.lang.reflect.Method.invokeNative(Native Method)
12-14 19:36:56.072: E/AndroidRuntime(540):  at java.lang.reflect.Method.invoke(Method.java:511)
12-14 19:36:56.072: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 19:36:56.072: E/AndroidRuntime(540):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 19:36:56.072: E/AndroidRuntime(540):  at dalvik.system.NativeStart.main(Native Method)
12-14 19:36:56.072: E/AndroidRuntime(540): Caused by: java.lang.NullPointerException
12-14 19:36:56.072: E/AndroidRuntime(540):  at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:39)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.Activity.performCreate(Activity.java:4465)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 19:36:56.072: E/AndroidRuntime(540):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 19:36:56.072: E/AndroidRuntime(540):  ... 11 more
12-14 19:36:59.042: I/Process(540): Sending signal. PID: 540 SIG: 9
12-14 19:37:06.032: D/AndroidRuntime(556): Shutting down VM
12-14 19:37:06.032: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 19:37:06.042: E/AndroidRuntime(556): FATAL EXCEPTION: main
12-14 19:37:06.042: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.os.Looper.loop(Looper.java:137)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 19:37:06.042: E/AndroidRuntime(556):  at java.lang.reflect.Method.invokeNative(Native Method)
12-14 19:37:06.042: E/AndroidRuntime(556):  at java.lang.reflect.Method.invoke(Method.java:511)
12-14 19:37:06.042: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 19:37:06.042: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 19:37:06.042: E/AndroidRuntime(556):  at dalvik.system.NativeStart.main(Native Method)
12-14 19:37:06.042: E/AndroidRuntime(556): Caused by: java.lang.NullPointerException
12-14 19:37:06.042: E/AndroidRuntime(556):  at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:39)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.Activity.performCreate(Activity.java:4465)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 19:37:06.042: E/AndroidRuntime(556):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 19:37:06.042: E/AndroidRuntime(556):  ... 11 more

The second logcat after applying ρяσѕρєя K’s solution:-

12-14 20:19:02.227: D/AndroidRuntime(537): Shutting down VM
12-14 20:19:02.237: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
12-14 20:19:02.247: E/AndroidRuntime(537): FATAL EXCEPTION: main
12-14 20:19:02.247: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mbawardi.layan/com.mbawardi.layan.MainActivity}: java.lang.NullPointerException
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.os.Looper.loop(Looper.java:137)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.main(ActivityThread.java:4340)
12-14 20:19:02.247: E/AndroidRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)
12-14 20:19:02.247: E/AndroidRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:511)
12-14 20:19:02.247: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-14 20:19:02.247: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-14 20:19:02.247: E/AndroidRuntime(537):  at dalvik.system.NativeStart.main(Native Method)
12-14 20:19:02.247: E/AndroidRuntime(537): Caused by: java.lang.NullPointerException
12-14 20:19:02.247: E/AndroidRuntime(537):  at com.mbawardi.layan.MainActivity.onCreate(MainActivity.java:42)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.Activity.performCreate(Activity.java:4465)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-14 20:19:02.247: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-14 20:19:02.247: E/AndroidRuntime(537):  ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:17:10+00:00Added an answer on June 16, 2026 at 1:17 am

    remove import android.R; from imports and import your on project R file . or use

    descriptor = MainActivity.this.getAssets().openFd(mediaFile);
    

    instead of

    descriptor = MainActivity.this.getAssets().openFd("audioogg/"+mediaFile);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner question here. I'm going to make a Jquery function that is used to
Beginner here, I have a simple question. In Android what would be the best
Beginner with MATLAB here. I'm trying to write a class that will load in
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
A beginner's headache: I am trying to scroll to the very top of my
Beginner here! I am trying to write a query that will select the 3
Beginner programmer here....hope it makes sense :) I have created a console app that
Beginner here trying to get a pipeline working in bash. If somebody can see
Beginner in Android development. My code crashes. I have made a simple Java method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.