-3

So basically, i want a button, that when clicked on plays the sound. Here is the code.

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button=(Button)this.findViewById(id.button1);

    final MediaPlayer mp = MediaPlayer.create(this, R.raw.lifesucks);   
    //mp.start();       
    button.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {


        }
    }); 

If the onclicklistener part is commented out and the mp.start() is not, it does play fine. When the onclicklistener is active, the app crashes upon loading. Same happens when i put the mp.start() under onClick ( where it should really be logically)

Edit: Sorry i am a newbie, here is the LogKat:

08-25 16:58:56.520: D/AndroidRuntime(5303): Shutting down VM
08-25 16:58:56.520: W/dalvikvm(5303): threadid=1: thread exiting with uncaught exception (group=0x41a86ba8)
08-25 16:58:56.530: E/AndroidRuntime(5303): FATAL EXCEPTION: main
08-25 16:58:56.530: E/AndroidRuntime(5303): Process: com.example.nohomo, PID: 5303
08-25 16:58:56.530: E/AndroidRuntime(5303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nohomo/com.example.nohomo.MainActivity}: java.lang.NullPointerException
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.os.Looper.loop(Looper.java:136)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at java.lang.reflect.Method.invokeNative(Native Method)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at java.lang.reflect.Method.invoke(Method.java:515)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at dalvik.system.NativeStart.main(Native Method)
08-25 16:58:56.530: E/AndroidRuntime(5303): Caused by: java.lang.NullPointerException
08-25 16:58:56.530: E/AndroidRuntime(5303):     at com.example.nohomo.MainActivity.onCreate(MainActivity.java:30)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.Activity.performCreate(Activity.java:5231)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-25 16:58:56.530: E/AndroidRuntime(5303):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-25 16:58:56.530: E/AndroidRuntime(5303):     ... 11 more

And the 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nohomo.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="18dp"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:text="Az élet szar!" />

</RelativeLayout>
VikingPingvin
  • 232
  • 5
  • 13

1 Answers1

1

Not sure how this compiles. Is this line a typo?

Button button=(Button)this.findViewById(id.button1);

should be

Button button=(Button)this.findViewById(R.id.button1);

If this is not the issue, please post your layout file.

r2DoesInc
  • 3,704
  • 3
  • 27
  • 58