android studio为每个循环导致崩溃与未捕获的exeption

时间:2018-04-18 04:59:18

标签: java sqlite android-studio

在这个android工作室应用程序的每个循环中,我没有捕获任何异常,但每当包含它的页面加载它崩溃我无法弄清楚为什么。任何帮助将不胜感激

这对于每个循环都进行了比较并将布尔值与上面的switch语句中的mat的日期进行比较,然后显示在特定日期需要采取警报的那些药丸

我对编程非常陌生,所以请怜悯

package com.example.josep.loginactivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Home extends AppCompatActivity {



    //private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    setTitle("Pill List");
                    Intent intent = new Intent(Home.this, PillListActivity.class);
                    startActivity(intent);
                    return true;
                case R.id.navigation_dashboard:
                    setTitle("Add a New Pill");
                    Intent intent2 = new Intent(Home.this, AddPill.class);
                    startActivity(intent2);
                    return true;
                case R.id.navigation_notifications:
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        //mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
         int mine;
        switch (day) {
            case Calendar.SUNDAY:
                mine =6;
                break;
            case Calendar.MONDAY:
                mine =0;
                break;

            case Calendar.TUESDAY:
                mine=1;
                break;

            case Calendar.WEDNESDAY:
                mine=2;
                break;

            case Calendar.THURSDAY:
                mine=3;
                break;
            case Calendar.FRIDAY:
                mine=4;
                break;

            case Calendar.SATURDAY:
                mine=5;
                break;
        }
        String set = "";



        DBhelper dbPill = new DBhelper(this);
        List<pillModel> pillList = dbPill.getAllPills();

        for(pillModel p:  pillList)
        {
            Boolean[]find = p.getDays();
            for (int i =0; i<find.length; i++)
            if(find[i])
            {
                set += p.getName() ;
                set += " ";
            }
        }

/*

       AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("today you need to take "+ set);

        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //perform any action
                Toast.makeText(getApplicationContext(), "Thank YOu", Toast.LENGTH_SHORT).show();
            }
        });


*/
        //creating alert dialog
        /*AlertDialog alertDialog = builder.create();
        alertDialog.show();
*/




    }
}

DBhelper类

package com.example.josep.loginactivity;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class DBhelper extends SQLiteOpenHelper {


        // Database Name
        private static final String DATABASE_NAME = "motherDB2";
        // Database Version
        private static final int DATABASE_VERSION = 2;

        // Table Names
        //private static final String TABLE_POLL = "Poll";
        //private static final String TABLE_VOTER = "voter";
        private static final String TABLE_USER = "user";
       private static final String TABLE_PILL = "pill";
      // private static final String TABLE_APPT = "appt";



        //  create  Tables for poll, voter, and user
        //private static final String CREATE_TABLE_POLL = "CREATE TABLE "  + TABLE_POLL+ " (CANDIDATEID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT, PARTY TEXT, STATE TEXT, ELECTIONYEAR INTEGER)";
        //private static final String CREATE_TABLE_VOTER = "CREATE TABLE "  + TABLE_VOTER+ " (VOTERID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT, LASTNAME TEXT, STATE TEXT, ADDRESS TEXT, ZIPCODE TEXT)";
       // private static final String CREATE_TABLE_APPT = "CREATE TABLE "  + TABLE_APPT+ " (APPTID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, EMAIL TEXT, DATE TEXT, COMMENT TEXT)";
        private static final String CREATE_TABLE_USER = "CREATE TABLE "  + TABLE_USER+ " (USERID INTEGER PRIMARY KEY AUTOINCREMENT, EMAIL TEXT, PASSWORD TEXT)";
       private static final String CREATE_TABLE_PILL = "CREATE TABLE "  + TABLE_PILL+
               " (PILLID INTEGER PRIMARY KEY AUTOINCREMENT, PILL TEXT, DOSE INTEGER, MONDAY BOOLEAN, TUESDAY BOOLEAN, WEDNESDAY BOOLEAN, THURSDAY BOOLEAN, FRIDAY BOOLEAN, SATURDAY BOOLEAN, SUNDAY BOOLEAN, ACTIVE BOOLEAN)";

        public DBhelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }




    @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase)
        {
            // creating required tables
            //sqLiteDatabase.execSQL(CREATE_TABLE_POLL);
            //sqLiteDatabase.execSQL(CREATE_TABLE_VOTER);
           sqLiteDatabase.execSQL(CREATE_TABLE_PILL);
            sqLiteDatabase.execSQL(CREATE_TABLE_USER);
           // sqLiteDatabase.execSQL(CREATE_TABLE_DEAL);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion)
        {
            // on upgrade drop older tables
            //sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_POLL);
            //sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_VOTER);
            sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
           sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_PILL);
           // sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_APPT);
            // create new tables
            onCreate(sqLiteDatabase);
        }

        // ------------------------ "POLL" table methods ----------------//

    public boolean insertPill(pillModel pill)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("PILL", pill.getName());
        values.put("DOSE", pill.getDoses());
        values.put("MONDAY", pill.isMonday());
        values.put("TUESDAY", pill.isTuesday());
        values.put("WEDNESDAY", pill.isWednesday());
        values.put("THURSDAY", pill.isThursday());
        values.put("FRIDAY", pill.isFriday());
        values.put("SATURDAY", pill.isSaturday());
        values.put("SUNDAY", pill.isSunday());
        values.put("ACTIVE", pill.isActive());

        long result  = db.insert(TABLE_PILL, null, values );
        if(result == -1)
            return false;
        else
            return true;

    }

    public String getPillInfo(pillModel pill){
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "Select PILL , DOSE, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,"+
                "SATURDAY ,SUNDAY , ACTIVE  FROM " + TABLE_PILL ;
        Cursor resultSet = db.rawQuery(query, null);
        if(resultSet.getCount()== 0)
            return null;
        else
            return  query;
    }
    public List<pillModel> getAllPills()
    {
        List<pillModel> pillList = new ArrayList<pillModel>();
        String selectQuery = "SELECT  * FROM " + TABLE_PILL;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor resultSet = db.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (resultSet.moveToFirst())
        {
            do
            {
                pillModel pill = new pillModel();
                pill.setPillID((resultSet.getInt(resultSet.getColumnIndex("PILLID"))));
                pill.setName((resultSet.getString(resultSet.getColumnIndex("PILL"))));
                pill.setDoses((resultSet.getInt(resultSet.getColumnIndex("DOSE"))));
                pill.setMonday(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("MONDAY")))));
                pill.setTuesday(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("TUESDAY")))));
                pill.setWednesday(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("WEDNESDAY")))));
                pill.setThursday(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("THURSDAY")))));
                pill.setFriday(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("FRIDAY")))));
                pill.setSaturday(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("SATURDAY")))));
                pill.setSunday(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("SUNDAY")))));
                pill.setActive(Boolean.parseBoolean((resultSet.getString(resultSet.getColumnIndex("ACTIVE")))));

                // adding to poll list
                pillList.add(pill);
            } while (resultSet.moveToNext());
        }
        return pillList;
    }

        public boolean insertUser(userModel user)
        {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("EMAIL", user.getEmail());
            values.put("PASSWORD", user.getPass());
            long result  = db.insert(TABLE_USER, null, values );
            if(result == -1)
                return false;
            else
                return true;

        }
        public Boolean getLoginInfo(userModel user){
            SQLiteDatabase db = this.getReadableDatabase();
            String query = "Select EMAIL, PASSWORD FROM " + TABLE_USER + " WHERE EMAIL = '"+user.getEmail() +"' AND PASSWORD= '"+user.getPass()+"'";
            Cursor resultSet = db.rawQuery(query, null);
            if(resultSet.getCount()== 0)
                return false;
            else
                return true;
        }
        public String getDose(String pill)
        {
            String query = "Select PILL FROM " + TABLE_PILL + " WHERE PILL = '"+pill +"'";
            return query;
        }
       public void addDose(String pill){
            SQLiteDatabase db = this.getWritableDatabase();
            String dose = "Select DOSE FROM " + TABLE_PILL + " WHERE PILL = '"+pill +"'";
            int house = Integer.parseInt(dose);
            house+=1;
            String mind =""+house;
            String query = "Update DOSE FROM " + TABLE_PILL + " SET DOSE = '"+mind +"'";

        }

    public void romoveDose(String pill){
        SQLiteDatabase db = this.getWritableDatabase();
        String dose = "Select DOSE FROM " + TABLE_PILL + " WHERE PILL = '"+pill +"'";
        int house = Integer.parseInt(dose);
        house-=1;
        String mind =""+house;
        String query = "Update " + TABLE_PILL + " SET DOSE = '"+mind +"'";
        //db.update("Update"+TABLE_PILL+ " SET DOSE="+ mind + "WHERE PILL="+pill);
    }
    }

丸模型课

package com.example.josep.loginactivity;

public class pillModel {


    private String name;
    private int Doses;
    private int pillID;
    private boolean monday;
    private boolean tuesday;
    private boolean wednesday;
    private boolean thursday;
    private boolean friday;
    private boolean saturday;
    private boolean sunday;
    private boolean active;

    public void setPillID(int id)
    {
        this.pillID = id;
    }
    public int getpillID()
    {
        return pillID;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setDoses(int Doeses)
    {
        this.Doses = Doses;
    }

    public Integer getDoses()
    {
        return Doses;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean isSunday() {
        return sunday;
    }

    public void setSunday(boolean sunday) {
        this.sunday = sunday;
    }

    public boolean isSaturday() {
        return saturday;
    }

    public void setSaturday(boolean saturday) {
        this.saturday = saturday;
    }

    public boolean isFriday() {
        return friday;
    }

    public void setFriday(boolean friday) {
        this.friday = friday;
    }

    public boolean isThursday() {
        return thursday;
    }

    public void setThursday(boolean thursday) {
        this.thursday = thursday;
    }

    public boolean isWednesday() {
        return wednesday;
    }

    public void setWednesday(boolean wednesday) {
        this.wednesday = wednesday;
    }

    public boolean isTuesday() {
        return tuesday;
    }

    public void setTuesday(boolean tuesday) {
        this.tuesday = tuesday;
    }

    public boolean isMonday() {
        return monday;
    }

    public void setMonday(boolean monday) {
        this.monday = monday;
    }

    public Boolean[]  getDays()
    {
        Boolean[]Days =new Boolean[6];
        String house = getName();
        if(isActive() && isMonday())
        {
            Days[0]=true;

        }
        if(isActive() && isTuesday())
        {
            Days[1]=true;

        }
        if(isActive() && isWednesday())
        {
            Days[2]=true;

        }
        if(isActive() && isThursday())
        {
            Days[3]=true;
        }
        if(isActive() && isFriday())
        {
            Days[4]=true;

        }
        if(isActive() && isSaturday())
        {
            Days[5]=true;

        }
        if(isActive() && isSunday())
        {
            Days[6]=true;

        }
        return Days;
    }

    public pillModel() {

    }

    public pillModel(String name, int Doses, boolean monday, boolean tuesday, boolean wednesday, boolean thursday,
                     boolean friday, boolean saturday, boolean sunday, boolean active) {

        this.name = name;
        this.Doses = Doses;
        this.monday = monday;
        this.tuesday = tuesday;
        this.wednesday = wednesday;
        this.thursday = thursday;
        this.friday = friday;
        this.saturday = saturday;
        this.sunday = sunday;
        this.active = active;
    }
}

0 个答案:

没有答案