0

i have tried to make a simple app . This is my first app in android , but i am not new to java . The app has just a customised background and a sound (Song) playing on homescreen . It builds successfully . No errors at all..!!! I have even taken care of warnings..

but this app doesn't work on the device (I dont use emulator..!!) It says , "Unfortunately [appname] has stopped "..

Why????

Here is my java code..:

package com.exmple.worth;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    MediaPlayer bfmv = MediaPlayer.create(MainActivity.this, R.raw.saf ) ;
    Button b1 ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.b1) ;
        bfmv.start() ;

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                bfmv.stop() ;
            }
        });
    }

}

here is my xml file :

<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="${packageName}.${activityClass}" 
    android:background="@drawable/qwe" >"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:text="Pause" />

</RelativeLayout>

and here is Android Manifest :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exmple.worth"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.exmple.worth.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Never Back Down
  • 138
  • 2
  • 12

1 Answers1

0

I tried your code. The way you instantiate bfmv is wrong.

MediaPlayer bfmv = MediaPlayer.create(MainActivity.this, R.raw.saf ) ;

It should be like this: On outer class just declare the object:

MediaPlayer bfmv;

and on your oncreate method instantiate it:

bfmv = MediaPlayer.create(MainActivity.this, R.raw.saf ) ;

The reason for force close is that the context MainActivity.this is null before oncreate is called.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61