简体   繁体   English

Android蓝牙聊天应用程序和发送Spotify歌曲名称

[英]Android Bluetooth Chat App & Sending Spotify Song Names

I'm am fairly new to Android, and I'm trying to develop an app for a small project of mine using an example Bluetooth Chat App and Spotify Media Notifications. 我是Android的新手,我正尝试使用示例蓝牙聊天应用程序和Spotify媒体通知为我的一个小项目开发应用程序。 I could not provide the links because stack overflow did not let me but they are available online with a quick google search. 我无法提供链接,因为堆栈溢出不让我使用,但可以通过快速的Google搜索在线获取它们。 I was wondering why I was getting this exception and is there any solution to this? 我想知道为什么我会收到此异常,对此有什么解决方案吗?

java.lang.NullPointerException java.lang.NullPointerException

The line numbers correspond to each of the pieces of code below: 行号与以下每个代码段相对应:

Line 269 if (message.length() > 0) 第269行,如果(message.length()> 0)

Line 56 public class BluetoothChatFragment extends Fragment 第56行公共类BluetoothChatFragment扩展Fragment

Line 131 sendMessage(artistName); 第131行sendMessage(artistName);

How my code is set up: 我的代码是如何设置的:

I have a static broadcast receiver set up exactly like the Spotify link below. 我有一个完全类似于下面的Spotify链接的静态广播接收器。 I grab the song name and artist and package them into strings. 我抓起歌曲名称和艺术家,然后将它们打包成字符串。 Then I have another broadcast receiver in my Bluetooth fragment. 然后,我的蓝牙片段中还有另一个广播接收器。 I used another one because I did not know how to get the string into the Bluetooth fragment because I needed to call sendMessage() a private method in the fragment class. 我使用了另一个,因为我不知道如何将字符串放入蓝牙片段,因为我需要在片段类中调用sendMessage()一个私有方法。 It seems like the exception is throwing when I call sendMessage(string) in my fragment. 当我在片段中调用sendMessage(string)时,似乎抛出了异常。

Static Spotify Broadcast Receiver 静态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);

    }
}

Broadcast Receiver in Fragment: 片段中的广播接收器:

    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 Method(): 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);
    }
}

Any help would be much appreciated! 任何帮助将非常感激!

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

To me it looks like sendMessage gets called with null instead of an actual string. 在我看来,sendMessage是用null而不是实际字符串调用的。 Note that intent.getStringExtra(some_string) can return null, if some_string cannot be found. 请注意,如果找不到some_string,则intent.getStringExtra(some_string)可以返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM