This question already has an answer here:
I have a program that I've developed for students that allows them to login and view their Univeristy information.
One feature I'm trying to introduce is when the user logs in, it displays what modules they have that day, if any.
Here is what's in my MainActivity.Java
- In this it gets the modules depending on what user is logged in, gets the current day, then uses those variables to use the getModuleTime
method. It then checks if they're empty and if they are it tells them they don't have seminars/lectures.
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
Intent appIntent;
Boolean loginSuc = false;
int loginSuccess = 0;
boolean sessionBool = false;
DB db = new DB(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
//Clock - http://ift.tt/1UQHg2Q
Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR);
boolean sessionBool = false;
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
Button actBTN=(Button)findViewById(R.id.myaccountbtn);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button logout=(Button)findViewById(R.id.logout);
ToggleButton toggle = (ToggleButton)findViewById(R.id.toggleButton1);
logout.setVisibility(View.GONE);
//Build Database - ensures it only runs once to avoid multiple data
boolean mboolean = false;
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
mboolean = settings.getBoolean("FIRST_RUN", false);
if (!mboolean) {
db.createstudents();
db.createmodules();
settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("FIRST_RUN", true);
editor.commit();
}
//Get user info from database & Display in Logcat.
List<tableStudents> outputList = db.getData();
List<tableModules> moduleList = db.getModules();
Log.d("result_list", "List created");
for (tableStudents student: outputList) {
Log.d("Username+PW Combos","Username: " + student.cnumber.toString() + " Password: " + student.password.toString());
}
for (tableModules module: moduleList)
{
Log.d(module.modulename.toString(), module.modulelecturer.toString());
}
}
protected void onResume() {
super.onResume();
final Intent intent = getIntent();
//If statement triggered when user logs in only.
if (loginSuccess == 0)
{
if (intent.hasExtra("loginSuccess")) {
loginSuccess++;
//Store session name
boolean loginSuccess = intent.getBooleanExtra("loginSuccess", true);
String sessionName = intent.getStringExtra("sessionName");
Toast.makeText(MainActivity.this, "Welcome " + sessionName.toString(), Toast.LENGTH_LONG).show();
TextView loggedinas = (TextView)findViewById(R.id.loggedinas);
loggedinas.setText(sessionName);
//User has logged in, changing login button to logout button.
Button login=(Button)findViewById(R.id.log);
Button logout=(Button)findViewById(R.id.logout);
login.setVisibility(View.GONE);
logout.setVisibility(View.VISIBLE);
//Set today's events
List<tableStudents> studentModules = db.getStudentsModules(sessionName);
for (tableStudents session: studentModules)
{
//Save modules to string
String module1 = session.modules.toString();
String module2 = session.modules1.toString();
String module3 = session.modules2.toString();
String today = getCurrentDay().toString();
Toast.makeText(MainActivity.this, "Today is: " + today, Toast.LENGTH_LONG).show();
TextView event1 = (TextView)findViewById(R.id.event1);
event1.setText(today + "'s Events:");
List<tableModules> moduleDayList = db.getModuleTime(module1, module2, module3, today);
for (tableModules module: moduleDayList) {
if(!module.modulelecturedate.isEmpty())
{
event1.setText("Your Lecture for" + module.modulename.toString() + "is at" + module.modulelecturetime.toString());
}
if (!module.moduleseminardate.isEmpty())
{
event1.setText("Your Seminar for" + module.modulename.toString() + "is at" + module.moduleseminartime.toString());
}
else
{
event1.setText("You have no seminars/lectures today");
}
}
}
}
}
}
public static String getCurrentDay(){
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.UK);
Calendar calendar = Calendar.getInstance();
return dayFormat.format(calendar.getTime());
}
public static String getDayName(int day){
switch(day){
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
return "ERROR";
}
public void goModules(View v)
{
final Intent intent2 = getIntent();
if (intent2.hasExtra("sessionName"))
{
Intent intent = new Intent(MainActivity.this, Modules.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this, "You haven't logged in yet", Toast.LENGTH_LONG).show();
}
}
public void goWeb(View v)
{
Intent intent = new Intent(MainActivity.this, Webview.class);
startActivity(intent);
}
public void goCalendar(View v)
{
Intent intent = new Intent(MainActivity.this, Calendarr.class);
startActivity(intent);
}
public void goLogin(View v)
{
Intent intent = new Intent(MainActivity.this, Login.class);
startActivity(intent);
}
public void goHomepage(View v)
{
Intent intent = new Intent(MainActivity.this, Homepage.class);
startActivity(intent);
}
public void goAccount(View v)
{
final Intent intent2 = getIntent();
if (intent2.hasExtra("sessionName"))
{
Intent intent = new Intent(MainActivity.this, MyAccount.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this, "You haven't logged in yet", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the getModuleTime
method in the DB.Java
class.
This creates a list from an SQL statement and I've tried to incorporate error checking so that if anything is null it will state it, however i think there is a flaw in my error checking as it is displaying a null reference error.
1) Does this suggest that the SQL statement is not finding any information?
2) If this is the case, how do I structure my if statements so that it can detect if they don't have anything that day?
public List<tableModules> getModuleTime(String module1, String module2, String module3, String day) {
List<tableModules> moduleList = new ArrayList<tableModules>();
// Select All Query
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT modulename, modulelecturetime, moduleseminartime FROM " + Table2 + " WHERE (modulename=? OR modulename=? OR modulename=?) AND (modulelecturedate=? OR moduleseminardate=?)", new String[]{module1, module2, module3, day, day});
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
tableModules module = new tableModules();
module.modulename = cursor.getString(0);
module.modulelecturedate = cursor.getString(1);
module.moduleseminardate = cursor.getString(2);
moduleList.add(module);
} while (cursor.moveToNext());
}
else
{
Log.d("Cant", "Find any lectures today");
}
// return contact list
return moduleList;
}
Here is the logcat:
03-08 21:01:53.969: E/AndroidRuntime(2528): FATAL EXCEPTION: main
03-08 21:01:53.969: E/AndroidRuntime(2528): Process: com.example.project, PID: 2528
03-08 21:01:53.969: E/AndroidRuntime(2528): java.lang.RuntimeException: Unable to resume activity {com.example.project/com.example.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2989)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3020)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.ActivityThread.access$800(ActivityThread.java:151)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.os.Handler.dispatchMessage(Handler.java:102)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.os.Looper.loop(Looper.java:135)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.ActivityThread.main(ActivityThread.java:5257)
03-08 21:01:53.969: E/AndroidRuntime(2528): at java.lang.reflect.Method.invoke(Native Method)
03-08 21:01:53.969: E/AndroidRuntime(2528): at java.lang.reflect.Method.invoke(Method.java:372)
03-08 21:01:53.969: E/AndroidRuntime(2528): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
03-08 21:01:53.969: E/AndroidRuntime(2528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
03-08 21:01:53.969: E/AndroidRuntime(2528): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
03-08 21:01:53.969: E/AndroidRuntime(2528): at com.example.project.MainActivity.onResume(MainActivity.java:116)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.Activity.performResume(Activity.java:6076)
03-08 21:01:53.969: E/AndroidRuntime(2528): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2978)
03-08 21:01:53.969: E/AndroidRuntime(2528): ... 11 more
Aucun commentaire:
Enregistrer un commentaire