0

When I start the following app in Android emulator the app stops with the following error message : "The application Player (process kilbo.net) has stpped unexpectedly. Please try again." There are no errors, warnings and I can't find anything in debugger related to the problem. This is a common problem I have, not just with this app.

    package kilbo.net;

import java.io.IOException;

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

public class Player extends Activity implements Runnable, OnClickListener{

   private TextView Status;
   private ProgressBar progressBar;
   private Button StartMedia;
   private Button Stop;
   private MediaPlayer mp;      

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

       Status = (TextView) findViewById(R.id.Status);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        StartMedia = (Button) findViewById(R.id.StartMedia);
        Stop = (Button) findViewById(R.id.Stop);

//Removing this two lines "fixes" the problem. So the error is probably related to this
        StartMedia.setOnClickListener(this);
        Stop.setOnClickListener(this);                
    }        

   @Override
    public void onClick(View v) {
        if(v.equals(StartMedia)){
            if(mp != null && mp.isPlaying()) return;
            MediaPlayer mp = new MediaPlayer();
            try {
                mp.setDataSource("http://kilbo.net/musik/Norrskenet.mp3");
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mp.start();               
            Status.setText("playingMedia");         
            progressBar.setVisibility(ProgressBar.VISIBLE);
            progressBar.setProgress(0);
            progressBar.setMax(mp.getDuration());
            new Thread(this).start();
        }

        if(v.equals(Stop) && mp!=null){
            mp.stop();
            mp = null;            
            Status.setText("Stop");
            progressBar.setVisibility(ProgressBar.GONE);
        }

   }

    @Override
    public void run() {
        int CurrentPosition= 0;
        int total = mp.getDuration();
        while(mp!=null && CurrentPosition<total){
            try {
                Thread.sleep(1000);
                CurrentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e){
                return;
            }            
            progressBar.setProgress(CurrentPosition);
        }
    }


}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <Button id="@+id/Stop"  
            android:layout_width="125px" android:layout_height="50px"
            android:background="#FF222222" android:text="Start" />
  <Button id="@+id/StartMedia"  
            android:layout_width="125px" android:layout_height="50px"
            android:background="#FF222222" android:text="Start" />
   <ProgressBar id="@+id/progressBar"  
            android:layout_width="125px" android:layout_height="50px"
            android:background="#FF222222" android:text="Start"
            style="?android:attr/progressBarStyleHorizontal" />
  <TextView id="@+id/Status"  
            android:layout_width="125px" android:layout_height="50px"
            android:background="#FF222222" android:text="Start" />
</LinearLayout>
2
  • 3
    Find Logcat. Use it. Then fix the missing android:id attributes in layout/main.xml Feb 24, 2011 at 11:31
  • Does StartMedia.setOnClickListener(this); need a separate id in main.xml, or should I add something to <Button id="@+id/StartMedia" android:layout_width="125px" android:layout_height="50px" android:background="#FF222222" android:text="Start" />? I have found the onClick attribute, but what am I supposed to put there? android:onClick="onClick"
    – Tien
    Feb 24, 2011 at 13:22

2 Answers 2

0

I think you get a NPE when de Debugger tries: StartMedia.setOnClickListener(this);

You should try to find the Stacktrace.

1
  • A breakpoint here StartMedia.setOnClickListener(this); Tells me this: 02-24 12:06:17.818: ERROR/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{kilbo.net/kilbo.net.Player}: java.lang.NullPointerException But I cant figure out why.
    – Tien
    Feb 24, 2011 at 12:55
0

You say the error is

02-24 12:06:17.818: ERROR/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{kilbo.net/kilbo.net.Player}: java.lang.NullPointerException

Is your activity properly declared in your manifest?

1
  • I have more or less moved on since I asked the question. But if I'm not miss minded the problem was fixed by setting onClick in main.xml rather than in the java code.
    – Tien
    Mar 1, 2011 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.