I am trying to send certain characters from an "alarm notification" activity in the android device to a Bluetooth receiver. But when I try to send the data an exception is thrown that I am passing a null object through the write() method. I can't understand how the object is NULL when I am initializing it in the activity.
When I try to send the same character from within an activity different from "alarm notification" it works fine.
Is there some code within "alarm notification" activity that hinders this?
Please forgive my amateur coding skills and guide me in the right direction.
Thank you.
AlarmNotification.java
package com.example.android.cheesesquare;
/**
* Created by HARDIK SHARMA on 5/25/2018.
*/
import...
public class AlarmNotification extends Activity
{
private final String TAG = "AlarmMe";
private Ringtone mRingtone;
private Vibrator mVibrator;
private final long[] mVibratePattern = { 0, 500, 500 };
private boolean mVibrate;
private Uri mAlarmSound;
private long mPlayTime;
private Timer mTimer = null;
private Alarm mAlarm;
private DateTime mDateTime;
private TextView mTextView;
private PlayTimerTask mTimerTask;
BluetoothConnectionService mBluetoothConnection;
String transmit = "";
@Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.notification);
mDateTime = new DateTime(this);
mTextView = (TextView)findViewById(R.id.alarm_title_text);
readPreferences();
mRingtone = RingtoneManager.getRingtone(getApplicationContext(), mAlarmSound);
if (mVibrate)
mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
start(getIntent());
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG, "AlarmNotification.onDestroy()");
stop();
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Log.i(TAG, "AlarmNotification.onNewIntent()");
addNotification(mAlarm);
stop();
start(intent);
}
private void start(Intent intent)
{
mAlarm = new Alarm(this);
mAlarm.fromIntent(intent);
Log.i(TAG, "AlarmNotification.start('" + mAlarm.getTitle() + "')");
mTextView.setText(mAlarm.getTitle());
transmit = mAlarm.getTitle();
byte[] bytes = transmit.toUpperCase().getBytes(Charset.defaultCharset());
mBluetoothConnection.write(bytes);
mTimerTask = new PlayTimerTask();
mTimer = new Timer();
mTimer.schedule(mTimerTask, mPlayTime);
mRingtone.play();
if (mVibrate)
mVibrator.vibrate(mVibratePattern, 0);
}
private void stop()
{
Log.i(TAG, "AlarmNotification.stop()");
mTimer.cancel();
mRingtone.stop();
if (mVibrate)
mVibrator.cancel();
}
public void onDismissClick(View view)
{
finish();
}
private void readPreferences()
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mAlarmSound = Uri.parse(prefs.getString("alarm_sound_pref", "DEFAULT_RINGTONE_URI"));
mVibrate = prefs.getBoolean("vibrate_pref", true);
mPlayTime = (long)Integer.parseInt(prefs.getString("alarm_play_time_pref", "30")) * 1000;
}
private void addNotification(Alarm alarm)
{
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent activity;
Intent intent;
Log.i(TAG, "AlarmNotification.addNotification(" + alarm.getId() + ", '" + alarm.getTitle() + "', '" + mDateTime.formatDetails(alarm) + "')");
intent = new Intent(this.getApplicationContext(), AlarmMe.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
activity = PendingIntent.getActivity(this, (int)alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
notification = builder
.setContentIntent(activity)
.setSmallIcon(R.drawable.baseline_alarm_black_18dp)
.setAutoCancel(true)
.setContentTitle("Missed alarm: " + alarm.getTitle())
.setContentText(mDateTime.formatDetails(alarm))
.build();
notificationManager.notify((int)alarm.getId(), notification);
}
@Override
public void onBackPressed()
{
finish();
}
private class PlayTimerTask extends TimerTask
{
@Override
public void run()
{
Log.i(TAG, "AlarmNotification.PlayTimerTask.run()");
addNotification(mAlarm);
finish();
}
}
My Logcat:
11893-11893/com.example.android.cheesesquare E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.cheesesquare, PID: 11893
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.cheesesquare/com.example.android.cheesesquare.AlarmNotification}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService.write(byte[])' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService.write(byte[])' on a null object reference
at com.example.android.cheesesquare.AlarmNotification.start(AlarmNotification.java:224)
at com.example.android.cheesesquare.AlarmNotification.onCreate(AlarmNotification.java:89)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
I have been at this for days now. Please help!!!
EDIT Logcat after initializing BluetoothService.
FATAL EXCEPTION: main
Process: com.example.android.cheesesquare, PID: 7482
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.cheesesquare/com.example.android.cheesesquare.AlarmNotification}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService$ConnectedThread.write(byte[])' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.android.cheesesquare.BluetoothConnectionService$ConnectedThread.write(byte[])' on a null object reference
at com.example.android.cheesesquare.BluetoothConnectionService.write(BluetoothConnectionService.java:306)
at com.example.android.cheesesquare.AlarmNotification.start(AlarmNotification.java:120)
at com.example.android.cheesesquare.AlarmNotification.onCreate(AlarmNotification.java:82)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
You are not passing a null object through the write() method. Your mBluetoothConnection
object is null.
You didn't initialise mBluetoothConnection
in your activity