0

私のMainActivityからサービスを開始しました。その後、onStartCommand()でブロードキャストレシーバに登録します。しかし、サービスからバンドルまでの両方の放送受信者にString値を渡します。しかし、私は放送受信機のonReceiver()でNullpointerExceptionを取得しています。ここに私のアクティビティコードがあります。サービスからブロードキャスト受信者にデータを渡している間にNullpointerExceptionが発生する

@Override 
protected void onStart() { 
    super.onStart(); 
    if (bluetoothService == null) { 
     kolamDeviceDialog(); 
    } else { 
     Log.e("MenuBluetoothService", bluetoothService.getStatus() + ""); 
     if (!bluetoothService.getStatus()) { 
      kolamDeviceDialog(); 
     } 
    } 
} 

private void kolamDeviceDialog() { 
    MaterialDialog.Builder builder = new MaterialDialog.Builder(MenuActivity.this) 
      .title("Select Bot") 
      .items(scanTypes) 
      .itemsCallback(new MaterialDialog.ListCallback() { 
       @Override 
       public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) { 
        if (position == 0) { 
         isSmallBot = false; 
         KolamDevice kolamDevice = new KolamDevice(); 
         Intent intent = new Intent(MenuActivity.this, BluetoothService.class); 
         intent.putExtra("Address", kolamDevice.getBigBotAddress()); 
         intent.putExtra("Name", kolamDevice.getBigBotName()); 
         startService(intent); 
         bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
        } else { 
         isSmallBot = true; 
         KolamDevice kolamDevice = new KolamDevice(); 
         Intent intent = new Intent(MenuActivity.this, BluetoothService.class); 
         intent.putExtra("Address", kolamDevice.getSmallBotAddress()); 
         intent.putExtra("Name", kolamDevice.getSmallBotName()); 
         startService(intent); 
         bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
        } 
       } 
      }); 
    builder.show(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    if (mIsBound) { 
     unbindService(mConnection); 
     mIsBound = false; 
    } 
} 

ここは私のサービスコードです。

public class BluetoothService extends Service { 

private final IBinder mBinder = new MyBinder(); 
private SmoothBluetooth smoothBluetooth; 
private static Device device; 
String address, name; 
private List<Integer> mBuffer = new ArrayList<>(); 
private List<String> mResponseBuffer = new ArrayList<>(); 

public BluetoothService() { 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.e("BluetoothService", "onCreated"); 
    // startForeground(1, new Notification()); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Log.e("BluetoothService", "Killed"); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.e("BluetoothService", "Started"); 
    startForeground(1, new Notification()); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     address = extras.getString("Address"); 
     name = extras.getString("Name"); 
    } 
    Log.e("Address", address); 
    Log.e("name", name); 
    smoothBluetooth = new SmoothBluetooth(this); 
    smoothBluetooth.setListener(mListener); 

    startScheduleReceiver(); 
    startBootReceiver(); 

    if (smoothBluetooth.isBluetoothEnabled()) { 
     if (!smoothBluetooth.isConnected()) { 
      device = new Device(name, address, true); 
      smoothBluetooth.tryConnection(); 
     } 
    } else { 
     device = new Device(name, address, true); 
     smoothBluetooth.tryConnection(); 

    } 
    return Service.START_NOT_STICKY; 
} 

private void startBootReceiver() { 
    Intent broadcastIntent = new Intent(this, MyStartServiceReceiver.class); 
    Bundle bundle = new Bundle(); 
    broadcastIntent.setAction(BluetoothAdapter.ACTION_STATE_CHANGED); 
    bundle.putString("BotType", name); 
    broadcastIntent.putExtras(bundle); 
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager service = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    Calendar cal = Calendar.getInstance(); 
    // start 1 second after boot completed 
    cal.add(Calendar.SECOND, 1); 
    // fetch every 1 second 
    // InexactRepeating allows Android to optimize the energy consumption 
    service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender); 
} 

private void startScheduleReceiver() { 
    Intent broadcastIntent = new Intent(this, MyScheduleReceiver.class); 
    Bundle bundle = new Bundle(); 
    broadcastIntent.setAction(BluetoothAdapter.ACTION_STATE_CHANGED); 
    bundle.putString("BotType", name); 
    broadcastIntent.putExtras(bundle); 
    PendingIntent sender = PendingIntent.getBroadcast(this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager service = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    Calendar cal = Calendar.getInstance(); 
    // start 1 second after boot completed 
    cal.add(Calendar.SECOND, 1); 
    // fetch every 1 second 
    // InexactRepeating allows Android to optimize the energy consumption 
    service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, sender); 
} 


@Override 
public IBinder onBind(Intent arg0) { 
    return mBinder; 
} 

public class MyBinder extends Binder { 
    BluetoothService getService() { 
     return BluetoothService.this; 
    } 
} 


public boolean getStatus() { 
    Log.e("BluetoothServiceStatus", smoothBluetooth.isConnected() + ""); 
    return smoothBluetooth.isConnected(); 
} 

私の最初の放送受信機のコードです。ここ

public class MyStartServiceReceiver extends BroadcastReceiver { 


@Override 
public void onReceive(Context context, Intent intent) { 
    Log.e("BootReceiver", "Started"); 
    Bundle bundle = intent.getExtras(); 
    String name = bundle.getString("BotType"); 
    if (name.equalsIgnoreCase("TEAMRED1")) { 
     KolamDevice kolamDevice = new KolamDevice(); 
     Intent bigBotIntent = new Intent(context, BluetoothService.class); 
     bigBotIntent.putExtra("Address", kolamDevice.getBigBotAddress()); 
     bigBotIntent.putExtra("Name", kolamDevice.getBigBotName()); 
     context.startService(bigBotIntent); 
    } else { 
     KolamDevice kolamDevice = new KolamDevice(); 
     Intent smallBotIntent = new Intent(context, BluetoothService.class); 
     smallBotIntent.putExtra("Address", kolamDevice.getSmallBotAddress()); 
     smallBotIntent.putExtra("Name", kolamDevice.getSmallBotName()); 
     context.startService(smallBotIntent); 
    } 
} 

}

私の第2放送受信コードです。ここ

public class MyScheduleReceiver extends BroadcastReceiver { 

private String name, address; 

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 
    Bundle bundle = intent.getExtras(); 
    String smallBot = bundle.getString("BotType"); 
    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { 
     if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_ON) { 
      Log.e("BluetoothCondition", "On"); 
      Log.e("Bot", smallBot); 


     } else if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { 
      Log.e("BluetoothCondition", "Off"); 
      Log.e("Bot", smallBot); 
     } 
    } 
} 

}

logcat誤差もあります。

java.lang.RuntimeException: Unable to start receiver com.ignite.a01hw909350.kolamdemo.MyScheduleReceiver: java.lang.NullPointerException: println needs a message 
                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2645) 
                        at android.app.ActivityThread.access$1800(ActivityThread.java:154) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:135) 
                        at android.app.ActivityThread.main(ActivityThread.java:5290) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at java.lang.reflect.Method.invoke(Method.java:372) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
                       Caused by: java.lang.NullPointerException: println needs a message 
                        at android.util.Log.println_native(Native Method) 
                        at android.util.Log.e(Log.java:232) 
                        at com.ignite.a01hw909350.kolamdemo.MyScheduleReceiver.onReceive(MyScheduleReceiver.java:28) 
                        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2638) 
                        at android.app.ActivityThread.access$1800(ActivityThread.java:154)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:135)  
                        at android.app.ActivityThread.main(ActivityThread.java:5290)  
                        at java.lang.reflect.Method.invoke(Native Method)  
                        at java.lang.reflect.Method.invoke(Method.java:372) 

なぜ私が両方の放送受信機のonReceiver()でnullになっているのかを教えてください。

+0

をlogcat – shmakova

+0

私はそれを更新しました –

答えて

0

サービスから放送受信機への渡し値は問題があります。

使用この:レシーバの両方にとって、簡単な

してください:

Intent broadcastIntent = new Intent(this, MyStartServiceReceiver.class); 
broadcastIntent.setAction(BluetoothAdapter.ACTION_STATE_CHANGED); 
broadcastIntent.putExtra("BotType", name); 
//sendBroadcast(broadcastIntent); 

onReceive()

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { 
    String smallBot = intent.getExtras().getString("BotType"); 
    //do your stuff 
    } 
} 

の内側にも、この追加:あなたのエラーから表示

if(smallBot!= null) 
    Log.e("Bot", smallBot); 
+0

'Permission Denial:sendブロードキャスト(broadcastIntent)でこのエラーが発生するのは、pid = 23668、uid = 10401'から ブロードキャストを送信することはできません、android.bluetooth.adapter.action.STATE_CHANGED; –

+0

私は気づいていませんでした。あなたはこれを必要としません: 'sendBroadcast(broadcastIntent);' – rafsanahmad007

+0

はまだ 'nullpointerexception'前のエラーを取得しています。 –

関連する問題