2011-02-26 41 views
0

你好,我們已經做了一個媒體播放器。現在我想在我的通知區域放置一個xml文件,我爲此遵循這個流程,但是在通知開始時我得到了nullpointerexception。過程= 「:遠程」 機器人:在Android清單文件 nullpointerexception開始通知!

服務類= 「MDService」 機器人

  1. 附加服務名稱= 「MDService」>

  1. 創建MDSInterface.aidl如下

interface MDSInterface {void start(); void stop(); }

  1. 製成用於遠程視圖MDService類在我添加xml文件和下面的添加在通知區域是代碼

包org.streamingmusic.main;

import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service;導入 android.content.Intent;導入 android.os.DeadObjectException;導入 android.os.IBinder;導入 android.os.RemoteException;導入 android.widget.RemoteViews;

公共類MDService延伸服務 {

public static NotificationManager 

納米; private static final int NOTIFY_ID = 1;

@Override public void onCreate(){ super.onCreate(); nm(通知管理器) getSystemService(NOTIFICATION_SERVICE);

}

@覆蓋公共無效的onDestroy(){ nm.cancel(NOTIFY_ID); }

/** *上播放的歌曲設置通知 * @參數TRACKNAME *參數 ARTISTNAME * /公共無效 startnot(){通知 通知=新的通知( R.drawable.icon, 「SMS」 ,0); RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.mindback); notification.contentView = contentView; notification.flags | = Notification.FLAG_NO_CLEAR; notification.flags | = 通知。FLAG_ONGOING_EVENT;

  Intent intent = new Intent(getApplicationContext(), 

StreamingPlayer.class); PendingIntent contentIntent = PendingIntent.getActivity(this,0, intent,Intent.FLAG_ACTIVITY_NEW_TASK); notification.contentIntent = contentIntent; nm.notify(NOTIFY_ID,notification); nm.notify(NOTIFY_ID,notification); }公共的IBinder getBinder(){

return mBinder; 

}

公共最終MDSInterface.Stub mBinder =新MDSInterface.Stub(){ 公共無效停止()將拋出RemoteException {// TODO 汽車式生成方法存根 nm.cancel(NOTIFY_ID); } public void start()throws RemoteException {// TODO 自動生成方法存根 startnot(); }};

@覆蓋公共的IBinder onBind(意向爲arg0){// TODO 自動生成方法存根返回 mBinder; }}

  1. 在玩家活動中聲明MDService類的對象。

公共靜態MDSInterface mpInterface;

5.bind服務

getApplicationContext()bindService( 新意圖(getApplicationContext(), MDService.class) mConnection,Context.BIND_AUTO_CREATE)。

6.添加服務連接方法。

公共靜態ServiceConnection mConnection =新ServiceConnection() {公共無效 onServiceConnected(組件名稱名稱, 的IBinder服務){mpInterface = MDSInterface.Stub.asInterface((的IBinder) 服務); } public void onServiceDisconnected(ComponentName name){mpInterface = null; } };

請任何人告訴我我在想什麼?

actully我的目標是給予notifiacation區域上的功能,以便用戶可以播放下一首prev歌曲,並且還可以去應用程序。 表示在通知區域添加4個按鈕。如果任何機構有更好的方式,那麼請分享我欣賞的人。

謝謝。

+0

請修復您的代碼格式。這很直接。您可以將它複製到SO中,突出顯示它並按下CTRL-K。它現在的樣子是不可讀的。 – EboMike 2011-02-26 06:34:09

+0

您需要重新設置帖子的格式 - 這是不可讀的。再次粘貼您的代碼,但使用花括號按鈕進行格式化,而不是使用引號圖標。 – 2011-02-26 06:34:19

回答

1

誠然,沒有煩惱去讀你的代碼,這種事情通常是由於試圖調用從前一次調用返回的空對象的方法。

故事的道德總是在調用其方法之前檢查返回的對象是否爲非null!

如果它們爲空,則需要通過對象檢索調用找出並解決錯誤。

+0

嗨,朋友。謝謝 – milind 2011-03-01 05:32:29