2016-04-17 127 views
0

我對Android很新,我正在嘗試使用示例藍牙聊天應用程序和Spotify媒體通知爲我的一個小項目開發應用程序。我無法提供鏈接,因爲堆棧溢出沒有讓我,但他們可以在網上快速谷歌搜索。我想知道爲什麼我得到這個異常,並有任何解決方案嗎?Android藍牙聊天應用程序和發送Spotify歌名

java.lang.NullPointerException

行號對應於每個的下面的代碼段的:

269行,如果(message.length()> 0)56

線公共類BluetoothChatFragment延伸片段

Line 131 sendMessage(artistName);

我的代碼是如何設置:

我有一個靜態的廣播接收器設置酷似低於Spotify的鏈接。我抓住歌名和藝術家,並將它們打包成字符串。然後我的藍牙片段中有另一個廣播接收器。我使用了另一個,因爲我不知道如何將字符串放入藍牙片段,因爲我需要在片段類中調用sendMessage()私有方法。當我在我的片段中調用sendMessage(string)時,看起來像拋出異常。

靜態的Spotify廣播接收機

package com.example.android.bluetoothchat; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

import com.example.android.common.logger.Log; 

public class MyBroadcastReceiver extends BroadcastReceiver { 
static final class BroadcastTypes { 
    static final String SPOTIFY_PACKAGE = "com.spotify.music"; 
    static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged"; 
    static final String QUEUE_CHANGED = SPOTIFY_PACKAGE + ".queuechanged"; 
    static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged"; 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
    // This is sent with all broadcasts, regardless of type. The value is taken from 
    // System.currentTimeMillis(), which you can compare to in order to determine how 
    // old the event is. 
    long timeSentInMs = intent.getLongExtra("timeSent", 0L); 

    Intent sendBack = new Intent("Received"); 

    String action = intent.getAction(); 

    String artistName; 
    String trackName; 


    if (action.equals(BroadcastTypes.METADATA_CHANGED)) { 
     artistName = intent.getStringExtra("artist"); 
     trackName = intent.getStringExtra("track"); 
     String msg = "S a " + artistName + '\0'; 
     String msg2 = "S n " + trackName + '\0'; 
     Log.d("Song", msg); 
     Log.d("Artist", msg2); 

     sendBack.putExtra("Song", msg); 
     sendBack.putExtra("Artist", msg2); 



    } else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) { 
     boolean playing = intent.getBooleanExtra("playing", false); 
     // Do something with extracted information 
     if(playing) { 
      Log.d("Playing", "S p\0"); 
      sendBack.putExtra("Playing", "S p\0"); 
     } 
     else { 
      Log.d("Playing", "S s\0"); 
      sendBack.putExtra("Playing", "S s\0"); 
     } 

    } 
    context.sendBroadcast(sendBack); 

    } 
} 

廣播接收器中的片段:

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     String artistName = intent.getStringExtra("Song"); 
     String trackName = intent.getStringExtra("Artist"); 
     String playing = intent.getStringExtra("Playing"); 

     Log.d("Please", artistName); 
     Log.d("Please", trackName); 
     Log.d("Please", playing); 

     sendMessage(artistName); 
     sendMessage(trackName); 
     sendMessage(playing); 

     } 
    }; 

的sendMessage()的方法:

private void sendMessage(String message) { 
    // Check that we're actually connected before trying anything 
    if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) { 
     Toast.makeText(getActivity(), R.string.not_connected,  Toast.LENGTH_SHORT).show(); 
     return; 
    } 

    // Check that there's actually something to send 
    if (message.length() > 0) { 
     // Get the message bytes and tell the BluetoothChatService to write 
     byte[] send = message.getBytes(); 
     mChatService.write(send); 

     // Reset out string buffer to zero and clear the edit text field 
     mOutStringBuffer.setLength(0); 
     mOutEditText.setText(mOutStringBuffer); 
    } 
} 

任何幫助,將不勝感激!

http://developer.android.com/samples/BluetoothChat/index.html

回答

0

對我來說,它看起來像的sendMessage被調用與空值而不是實際的字符串。請注意,如果找不到some_string,則intent.getStringExtra(some_string)可以返回null。