2011-02-24 91 views
0

當我在Android模擬器中啓動以下應用程序時,應用程序停止並顯示以下錯誤消息:「應用程序播放器(進程kilbo.net)出現意外斷開,請重試。 沒有錯誤,警告,我在調試器中找不到與問題相關的任何內容。 這是我的一個常見問題,而不僅僅是這個應用程序。應用程序意外出現

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> 
+3

查找Logcat。用它。然後在layout/main.xml中修復丟失的android:id屬性 – 2011-02-24 11:31:31

+0

StartMedia.setOnClickListener(this);在main.xml中需要一個單獨的ID,或者我應該添加一些東西到

回答

0

我覺得你得到一個NPE時去調試器嘗試: StartMedia.setOnClickListener(本);

您應該嘗試查找Stacktrace。

+0

由於某種原因,我需要一個斷點來查看錯誤 – Tien 2011-02-24 12:52:17

+0

這裏有一個斷點StartMedia.setOnClickListener(this);告訴我這個:02-24 12:06:17.818:ERROR/AndroidRuntime(369):java.lang.RuntimeException:無法啓動活動ComponentInfo {kilbo.net/kilbo.net.Player}:java.lang.NullPointerException 但是我無法弄清楚爲什麼。 – Tien 2011-02-24 12:55:18

0

你說的錯誤是

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 

是您的活動在您的清單中正確聲明?

+0

自從我問這個問題以來,我已經或多或少地感動了。但是如果我不小心,通過在main.xml中設置onClick而不是在java代碼中解決了問題。 – Tien 2011-03-01 09:42:33