not able to insert data in sqlite database?

Saturday, May 3, 2014

hi am new to android and am creating an alarm i am not able to insert hour and minute in data base here the below is my all source code any one please help me to solve this problem . while running the program i am getting Nullpointer exception at insert method.



package com.toprecur.alarmactivity;

import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class AlarmActivity<DatabaseOpenHelper> extends Activity {
private static int hour;
private static int minute;
private static int id;
public static Object ArrayofName;
TimePicker myTimePicker;
Button buttonstartSetDialog;
Button buttonCancelAlarm;
Button buttonSound;
Button buttonSnooze;
TextView textAlarmPrompt;
private Context ctx;
private SQLiteDatabase mDB = null;
private DatabaseOpenHelper mDbHelper;
private SimpleCursorAdapter mAdapter;

TimePicker timePicker;
CheckBox optRepeat;
Intent intent;
AlarmManager alarmManager;
// private Intent mAlarmReciever;
private PendingIntent pendingIntent, pendingIntent1, pendingIntent2,
pendingIntent3, pendingIntent4, pendingIntent5, pendingIntent6,
pendingIntent7;
private CheckBox chk_monday, chk_tuesday, chk_wednesday, chk_thursday,
chk_friday, chk_saturday, chk_sunday;
final static int RQS_1 = 1;
private static final String TAG = AlarmActivity.class.getName();
protected static final LayoutInflater layoutInflater = null;

private static final String KEY_ALARM_HOUR = "hour";
private static final String KEY_ALARM_MINUTE = "minute";
private static final String KEY_ID = null;
private final String DATABASE_TABLE_NAME = "alarm";
private boolean timerflag = false;

String mypath = "/Settings/Sound/Phone ringtone/";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);

ctx = this;

timePicker = (TimePicker) findViewById(R.id.picker);
// optRepeat = (CheckBox) findViewById(R.id.optrepeat);

chk_monday = (CheckBox) findViewById(R.id.optrepeat1);

chk_tuesday = (CheckBox) findViewById(R.id.optrepeat2);
chk_wednesday = (CheckBox) findViewById(R.id.optrepeat3);
chk_thursday = (CheckBox) findViewById(R.id.optrepeat4);

chk_friday = (CheckBox) findViewById(R.id.optrepeat5);

chk_saturday = (CheckBox) findViewById(R.id.optrepeat6);

chk_sunday = (CheckBox) findViewById(R.id.optrepeat7);

textAlarmPrompt = (TextView) findViewById(R.id.alarmprompt);

buttonstartSetDialog = (Button) findViewById(R.id.startSetDialog);
buttonstartSetDialog.setOnClickListener(new OnClickListener() {

private String KEY_HOUR;
private String KEY_MINUTE;
private String DATABASE_TABLE_NAME;
private String[] minutes;

@Override
public void onClick(View v) {

Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();

calSet.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calSet.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calSet.set(Calendar.SECOND, 0);
// calSet.set(Calendar.MILL ISECOND, 0);

if (calSet.compareTo(calNow) <= 0) {
// Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
// SQLiteDatabase mDB = null;
AlarmActivity mDbHelper;

// clearAll();
// insertalarmclock();
// Cursor c = readalarm();
// mAdapter = ;

setListAdapter(mAdapter);
onChecked(calSet);
setAlarm(calSet);
}

// private void insertalarmclock() {
// ContentValues values = new ContentValues();
// values.get(this.KEY_HOUR);
// values.put(KEY_HOUR, hour);
// mDB.insert(this. DATABASE_TABLE_NAME, null, values);
// values.get(this.KEY_MINUTE);
// values.put(KEY_MINUTE, minute);
// mDB.insert(this. DATABASE_TABLE_NAME, null, values);
// values.clear();
// }

// private void clearAll() {
// SQLiteDatabase mDB = null;
// mDB.delete(MyDatabaseHelper.DATABASE_TABLE_NAME, null, null);
//
// }
private Cursor readalarm() {
return mDB.query(this.DATABASE_TABLE_NAME, this.minutes, null,
new String[] {}, null, null, null);
}
});

buttonCancelAlarm = (Button) findViewById(R.id.cancel);
buttonCancelAlarm.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

cancelAlarm();

}
});

buttonSound = (Button) findViewById(R.id.btn_sound);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);

buttonSound.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
AlarmActivity.this);

Log.d("TAG", "button inside mobile");

alertDialogBuilder
// .setMessage("Click yes to exit!")
.setCancelable(false).setPositiveButton("SdCard",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
String uri = null;
Intent intent = new Intent(
RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(
RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(
RingtoneManager.EXTRA_RINGTONE_TITLE,
"Select Tone");
// if (uri != null) {
// intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
// Uri.parse(uri));
// } else {
// intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
// (Uri) null);
// }
startActivityForResult(intent, 0);
}
});
alertDialogBuilder.show();
}
});

}

// protected SQLiteDatabase getWritableDatabase() {
// // TODO Auto-generated method stub
// return null;
// }

private void onChecked(final Calendar targetCal) {
chk_monday
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView == chk_monday) {
if (isChecked) {
intent = new Intent(getBaseContext(),
AlarmReciever.class);
pendingIntent1 = PendingIntent.getBroadcast(
getBaseContext(), 1, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
forday(1, pendingIntent1, targetCal);
Toast.makeText(ctx, "checked" + buttonView,
Toast.LENGTH_LONG).show();
}
}

}
});
chk_tuesday
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView == chk_tuesday) {
if (isChecked) {
intent = new Intent(getBaseContext(),
AlarmReciever.class);
pendingIntent2 = PendingIntent.getBroadcast(
getBaseContext(), 2, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
forday(2, pendingIntent2, targetCal);
Toast.makeText(ctx, "checked" + buttonView,
Toast.LENGTH_LONG).show();
}
}

}
});
chk_wednesday
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView == chk_wednesday) {
if (isChecked) {
intent = new Intent(getBaseContext(),
AlarmReciever.class);
pendingIntent3 = PendingIntent.getBroadcast(
getBaseContext(), 3, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
forday(3, pendingIntent3, targetCal);
Toast.makeText(ctx, "checked",
Toast.LENGTH_LONG).show();
}
}

}
});
chk_thursday
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView == chk_thursday) {
if (isChecked) {
intent = new Intent(getBaseContext(),
AlarmReciever.class);
pendingIntent4 = PendingIntent.getBroadcast(
getBaseContext(), 4, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
forday(4, pendingIntent4, targetCal);
Toast.makeText(ctx, "checked",
Toast.LENGTH_LONG).show();
}
}

}
});
chk_friday
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView == chk_friday) {
if (isChecked) {
intent = new Intent(getBaseContext(),
AlarmReciever.class);
pendingIntent5 = PendingIntent.getBroadcast(
getBaseContext(), 5, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
forday(5, pendingIntent5, targetCal);
Toast.makeText(ctx, "checked",
Toast.LENGTH_LONG).show();
}
}

}
});
chk_saturday
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView == chk_saturday) {
if (isChecked) {
intent = new Intent(getBaseContext(),
AlarmReciever.class);
pendingIntent6 = PendingIntent.getBroadcast(
getBaseContext(), 6, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
forday(6, pendingIntent6, targetCal);
Toast.makeText(ctx, "checked",
Toast.LENGTH_LONG).show();
}
}

}
});
chk_sunday
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView == chk_sunday) {
if (isChecked) {
intent = new Intent(getBaseContext(),
AlarmReciever.class);
pendingIntent7 = PendingIntent.getBroadcast(
getBaseContext(), 7, intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
forday(7, pendingIntent7, targetCal);
Toast.makeText(ctx, "checked",
Toast.LENGTH_LONG).show();
}
}

}
});

// AlertDialog.BUTTON_POSITIVE();
}

public void setListAdapter(SimpleCursorAdapter mAdapter) {
String[] alarm = new String[] { "select*from " + DATABASE_TABLE_NAME };
return ;
}

private void setAlarm(Calendar targetCal) {

intent = new Intent(getBaseContext(), AlarmReciever.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1,
intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
pendingIntent);
textAlarmPrompt.setText("\n\n***\n" + "Alarm is set@ "
+ targetCal.getTime() + "\n" + "Repeat every 5 minutes\n"
+ "***\n");
int hour = targetCal.get(Calendar.HOUR_OF_DAY);
int minute = targetCal.get(Calendar.MINUTE);
Log.d(TAG, "value" + hour + minute);

String h = Integer.toString(hour);
String m = Integer.toString(minute);
ContentValues values = new ContentValues();
values.put("hour", h);
values.put("minute", m);
insertalarmclock(h, m);
setListAdapter(mAdapter);
// private void clearAll() {
// SQLiteDatabase mDB = null;
// mDB.delete(MyDatabaseHelper.DATABASE_TABLE_NAME, null, null);
//
// }
}

public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXIST" + DATABASE_TABLE_NAME + ""
+ KEY_ID + "INTEGER AUTOINCRREMENT ," + KEY_ALARM_HOUR
+ "TIME," + KEY_ALARM_MINUTE + "TIME,)");
}

public long insertalarmclock(String h, String m) {
ContentValues values = new ContentValues();
values.put("hour", h);
values.put("minute", m);
String val = (String) values.get(h);
Log.d(TAG, "value" + val + h + m);

return mDB.insert(DATABASE_TABLE_NAME, null, values);

}

private void cancelAlarm() {

textAlarmPrompt.setText("\n\n***\n" + "Alarm Cancelled! \n" + "***\n");

Intent intent = new Intent(getBaseContext(), AlarmReciever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), RQS_1, intent, 0);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

}

public void forday(int week, PendingIntent pendingIntent, Calendar targetCal) {

Calendar calSet = Calendar.getInstance();

int hour = timePicker.getCurrentHour();

int minute = timePicker.getCurrentMinute();

calSet.set(Calendar.DAY_OF_WEEK, week);

calSet.set(Calendar.HOUR_OF_DAY, hour);

calSet.set(Calendar.MINUTE, minute);

calSet.set(Calendar.SECOND, 0);

calSet.set(Calendar.MILLISECOND, 0);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7,
pendingIntent);
textAlarmPrompt.setText("\n\n***\n" + "Alarm is set@ "
+ targetCal.getTime() + "\n" + "One shot\n" + "***\n");
}

public void inday(int day, PendingIntent pendingIntent, Calendar targetCal,
long remainingMilisecondsToTopHour) {

Calendar calSet = Calendar.getInstance();

int hour = timePicker.getCurrentHour();

int minute = timePicker.getCurrentMinute();

// calSet.set(Calendar.DAY_OF_WEEK, week);

calSet.set(Calendar.HOUR_OF_DAY, hour);

calSet.set(Calendar.MINUTE, minute);

calSet.set(Calendar.SECOND, 0);

calSet.set(Calendar.MILLISECOND, 0);
long firstTime = SystemClock.elapsedRealtime();
firstTime += remainingMilisecondsToTopHour;
long a = calSet.getTimeInMillis();

// Schedule the alarm!
AlarmManager am = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,
calSet.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), AlarmManager.INTERVAL_HOUR * 12,
pendingIntent);
textAlarmPrompt.setText("\n\n***\n" + "Alarm is set@ "
+ targetCal.getTime() + "\n" + "One shot\n" + "***\n");
}


}







http://ift.tt/1i7zT4j