程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android Studio:SQLite 某种形式的测验。在 logcat 中出现错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Android Studio:SQLite 某种形式的测验。在 logcat 中出现错误?

开发过程中遇到Android Studio:SQLite 某种形式的测验。在 logcat 中出现错误的问题如何解决?下面主要结合日常开发的经验,给出你关于Android Studio:SQLite 某种形式的测验。在 logcat 中出现错误的解决方法建议,希望对你解决Android Studio:SQLite 某种形式的测验。在 logcat 中出现错误有所启发或帮助;

我在运行我的应用程序时遇到错误。我应该从我创建的数据库中获取我的照片数据,并添加了这些值。我查看了所有可能的在线资源,当它说我正在调用的参数中没有数据时,我真的无法弄清楚为什么我得到空值。这是我第一次在这里提问,所以不要介意我不知道这些功能。也是我第一次使用 sqlite 和 androID studio。 FYP 截止日期即将到来,因此我很困:(

我的错误上线了 photoQuestion.setText(currentQuestion.getPhoto) 它说我数据库的那一列基本上没有数据

PeopleModeActivity(主要活动)

<!-- application.HTML.erb -->
<!-- JavaScript pack Tags loaded in head and my app component displays the template properly -->
<div ID="vue-app-root">
    <app></app>
</div>

sqliteDbHandler

public class PeopleModeActivity extends AppCompatActivity {

    private PeopleMode PEOPLEMODE;
    private PeopleModeQuestions currentQuestion; // current question that is accessed
    private int relativeS_ID;  // current relatives ID that is accessed (Question_ID)
    private sqliteDbHandler DbHandler;  // database handler

    private ArrayList<PeopleModeAnswers> currentQuestion_Answers;  // store List of answers to respective questions
    private PeopleModeAnswers relationshipOption_1;  // variable in reference to relationships option 1
    private PeopleModeAnswers relationshipOption_2;  // variable in reference to relationships option 2

    private TextVIEw photoQuestion; // UI for question (display photo)
    private button buttonOption1;  // UI for relationships option 1
    private button buttonOption2;  // UI for relationships option 2

    @OverrIDe
    protected voID onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentVIEw(R.layout.activity_people_mode);

        DbHandler = new sqliteDbHandler(this);

        // Obtain the Intent which contains extra values inserted in the prevIoUs Activity
        // The values to retrIEve and use: PEOPLEMODE,relativeS_ID
        // If game mode with same PatIEnt ID is going on,the values (such as questions and answers for that patIEnt ID will carry forward)
        Intent intent = getIntent();

        // Game mode intialisation
        PEOPLEMODE = intent.getParcelableExtra("PEOPLEMODE");

        // current relativeS_ID initialisation
        relativeS_ID = intent.getIntExtra("relativeS_ID",1);


        // To make sure that the game ends and goes to congratulations screen
        // when all questions of the ongoing patIEnt ID finishes
        // There are 4 questions (relatives_ID) in the PEOPLE MODE,so 4 will be used as counter to kNow when to end
        if (relativeS_ID > 4) {
            Intent congratulations_screen = new Intent(PeopleModeActivity.this,EndOfQuestionsActivity.class);
            congratulations_screen.putExtra("PEOPLEMODE",PEOPLEMODE);
            startActivity(congratulations_screen);
        }
        // there are still more questions (relatives_ID),people mode will continue and carry forward prevIoUs question data if any
        // such as current relatives_ID
        else {
            // current question initialisation (in reference to photo associated with current relativesID)
            // current questions based on current relativesID will be
            // stored in a PeopleModeQuestions object

            currentQuestion = DbHandler.getrelativesPhotoQuestion(relativeS_ID);  
 
            // RetrIEve the List that stores the answers related to the current question (relatives_ID)
            // List created in db will be called,getrelativesPhotoAnswersByrelativesID
            currentQuestion_Answers = (ArrayList) DbHandler.getrelativesPhotoAnswersByrelativesID(relativeS_ID);

            // Initialise all the relationships answers for the people mode
            relationshipOption_1 = currentQuestion_Answers.get(0);
            relationshipOption_1 = currentQuestion_Answers.get(1);

            // display the Photo (Question) in the screen  
            photoQuestion = (TextVIEw) findVIEwByID(R.ID.photoQuestiontest);
            photoQuestion.setText(currentQuestion.getPhoto());

            // display the relationships options (Answers) in the screen
            buttonOption1 = findVIEwByID(R.ID.option1);
            buttonOption1.setText(relationshipOption_1.getRelationship1());

            buttonOption2 = findVIEwByID(R.ID.option2);
            buttonOption2.setText(relationshipOption_1.getRelationship2());

            Onoption1ClickbuttonListener();
            Onoption2ClickbuttonListener();

        }
}
// Click Listener action for each option selected
    public voID Onoption1ClickbuttonListener() {
        buttonOption1.setonClickListener(new VIEw.OnClickListener() {
            @OverrIDe
            public voID onClick(VIEw v) {
                if (relationshipOption_1.getCorrectRelationship() == 1) {      // If correct,click next to go to next qn if any
                    AlertDialog.Builder correct = new AlertDialog.Builder(PeopleModeActivity.this);
                    correct.setMessage("You got it correct! \n Good Job!").setNeutralbutton("Next",new DialogInterface.OnClickListener() {
                        @OverrIDe
                        public voID onClick(DialogInterface dialog,int which) {
                            dialog.cancel();

                            Intent peopleMode_intent = new Intent(PeopleModeActivity.this,PeopleModeActivity.class);
                            peopleMode_intent.putExtra("PEOPLEMODE",PEOPLEMODE);
                            peopleMode_intent.putExtra("relativeS_ID",(relativeS_ID + 1));
                            startActivity(peopleMode_intent);

                            overrIDePendingTransition(0,0);
                        }
                    }).setCancelable(false);
                    AlertDialog alert_2 = correct.create();
                    correct.show();
                }

                if (relationshipOption_1.getCorrectRelationship() == 0) {    // If wrong,same question will be repeated
                    AlertDialog.Builder wrong = new AlertDialog.Builder(PeopleModeActivity.this);
                    wrong.setMessage("No,not correct! \n It's okay,Try Again!").setNeutralbutton("Do again",relativeS_ID);   // relatives_ID (Question ID) will stay the same since question will be repeated
                            startActivity(peopleMode_intent);

                            overrIDePendingTransition(0,0);
                        }
                    }).setCancelable(false);
                    AlertDialog alert_2 = wrong.create();
                    wrong.show();
                }
            }
        });
    }

    public voID Onoption2ClickbuttonListener() {
        buttonOption2.setonClickListener(new VIEw.OnClickListener() {
            @OverrIDe
            public voID onClick(VIEw v) {
                if (relationshipOption_2.getCorrectRelationship() == 1) {      // If correct,0);
                        }
                    }).setCancelable(false);
                    AlertDialog alert_2 = correct.create();
                    correct.show();
                }

                if (relationshipOption_2.getCorrectRelationship() == 0) {    // If wrong,0);
                        }
                    }).setCancelable(false);
                    AlertDialog alert_2 = wrong.create();
                    wrong.show();
                }
            }
        });


    }
}

    public class sqliteDbHandler extends sqliteOpenHelper {

    public static final int sqlITEDB_VERSION = 1;
    public static final String sqlITEDB_name = "fyp.db";

    // PatIEnt Details
    public static final String PatIEnt_Details_table = "PatIEntDetails";
    public static final String PatIEnt_Details_Column_ID = "ID";
    public static final String PatIEnt_Details_Column_name = "name";

    // PatIEnts' relatives (Question for "People" Game Mode)
    public static final String PatIEntDetails_relatives_table = "PatIEntDetails_relatives";
    public static final String PatIEntDetails_relatives_Column_relativesID = "relativesID";
    public static final String PatIEntDetails_relatives_Column_PatIEntID = "PatIEntID";
    public static final String PatIEntDetails_relatives_Column_name = "name";
    public static final String PatIEntDetails_relatives_Column_Photo = "Photo";

    // PatIEnts' Relationships with relatives (Answer for "People" Game Mode)
    public static final String PatIEntDetails_relatives_Relationships_table = "PatIEntDetails_relatives_Relationships";
    public static final String PatIEntDetails_relatives_Relationships_Column_RelationshipID = "RelationshipID";
    public static final String PatIEntDetails_relatives_Relationships_Column_relativesID = "relativesID";
    public static final String PatIEntDetails_relatives_Relationships_Column_Relationship = "Relationship";
    public static final String PatIEntDetails_relatives_Relationships_Column_CorrectRelationship = "CorrectRelationship";

    public sqliteDbHandler(Context context) {
        super(context,sqlITEDB_name,null,sqlITEDB_VERSION);
    }

// Populate database with data to support "People" Game Mode
@OverrIDe
public voID onCreate (sqliteDatabase db) {

    // Create table in preetifyp.db database for PatIEnt Details
    String PATIENT_DETAILS = "CREATE table " + PatIEnt_Details_table + "("
                                + PatIEnt_Details_Column_ID + " INTEGER PRIMARY KEY autoINCREMENT,"
                                + PatIEnt_Details_Column_name + " TEXT" + ")";
    db.execsql(PATIENT_DETAILS);

    // Create table in preetifyp.db database for PatIEnts' relatives (Questions for "People" Game Mode)
    String PATIENTDETAILS_relativeS = "CREATE table " + PatIEntDetails_relatives_table + " ( "
                                        + PatIEntDetails_relatives_Column_relativesID + " INTEGER PRIMARY KEY autoINCREMENT,"
                                        + PatIEntDetails_relatives_Column_PatIEntID + " INTEGER,"
                                        + PatIEntDetails_relatives_Column_name + " TEXT,"
                                        + PatIEntDetails_relatives_Column_Photo + " TEXT,"   // testing with TEXT to get the quiz running,change to INTEGER later
                                        + " FOREIGN KEY( " + PatIEntDetails_relatives_Column_PatIEntID + " ) REFERENCES " + PatIEnt_Details_table
                                                                                            + " ( " + PatIEnt_Details_Column_ID + " ) )";
    db.execsql(PATIENTDETAILS_relativeS);

    // Create table in preetifyp.db database for PatIEnts' Relationships with relatives (Answers for "People" Game Mode)
    String PATIENTDETAILS_relativeS_RELATIONSHIPS = "CREATE table " + PatIEntDetails_relatives_Relationships_table + " ( "
                                                        + PatIEntDetails_relatives_Relationships_Column_RelationshipID + " INTEGER PRIMARY KEY autoINCREMENT,"
                                                        + PatIEntDetails_relatives_Relationships_Column_relativesID + " INTEGER,"
                                                        + PatIEntDetails_relatives_Relationships_Column_Relationship + " TEXT,"
                                                        + PatIEntDetails_relatives_Relationships_Column_CorrectRelationship + " INTEGER,"  // Responses: 0 = no,1 = yes
                                                        + " FOREIGN KEY( " + PatIEntDetails_relatives_Relationships_Column_relativesID + " ) REFERENCES " + PatIEntDetails_relatives_table
                                                                                                                                + " ( " + PatIEntDetails_relatives_Column_relativesID + " ) )";
    db.execsql(PATIENTDETAILS_relativeS_RELATIONSHIPS);

   // Insert data for "People" Game Mode

    // Insert PatIEnt Details data
    db.execsql("INSERT INTO " + PatIEnt_Details_table + " VALUES (" + 1 + ",'Sandra' )");
    db.execsql("INSERT INTO " + PatIEnt_Details_table + " VALUES (" + 2 + ",'Tom' )");

    // Insert PatIEnts' relatives data (Questions)
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_table + " VALUES (" + 1 + "," + 1 + ",'john','photo of john' )" );
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_table + " VALUES (" + 2 + ",'susan','photo of susan' )" );
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_table + " VALUES (" + 3 + ",'mary','photo of mary' )" );
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_table + " VALUES (" + 4 + ",'Sarah','photo of Sarah' )" );

    // Insert PatIEnts' relatives' Relationships data (Answers)
    // First Question's Answer - John
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 1 + ",'Father',+ 1 )" );
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 2 + ",+ 0 )" );

    // Second Question's Answer - Susan
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 3 + "," + 2 + ",+ 0 )" );
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 4 + ",'Mother',+ 1 )" );

    // Third Question's Answer - Mary
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 5 + "," + 3 + ",'Sister',+ 1 )" );
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 6 + ",'FrIEnd',+ 0 )" );

    // Fourth Question's Answer - Sarah
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 7 + "," + 4 + ",+ 0 )" );
    db.execsql("INSERT INTO " + PatIEntDetails_relatives_Relationships_table + " VALUES (" + 8 + ",+ 1 )" );


}

    public voID addPhotos(PeopleModeQuestions photo){
        sqliteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(PatIEntDetails_relatives_Column_Photo,photo.getPhoto()); // Photo path
        // Inserting photo
        db.insert(PatIEntDetails_relatives_table,values);
        db.close();
    }

// Return all patIEnt names stored in database
    public List<String> getAllPatIEntnames() {
        List<String> patIEnts = new ArrayList<String>();

        // Select PatIEnt names data from table
        String selectPatIEntnames = "SELECT " + PatIEnt_Details_Column_name + " FROM " + PatIEnt_Details_table;
        sqliteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawquery(selectPatIEntnames,null);

        // looPing through all rows and adding to List
        if(cursor!=null && cursor.getCount() > 0) {
            if (cursor.movetoFirst()) {
                do {
                    patIEnts.add(cursor.getString(0));
                } while (cursor.movetoNext());
            }
        }
        cursor.close();
        db.close();

        return patIEnts;
    }

    public List<PeopleModeQuestions> getAllPeopleModeQuestions() {
        List<PeopleModeQuestions> listofrelativesPhotos = new ArrayList<PeopleModeQuestions>();
        String selectAllrelativesPhotos = "SELECT " + PatIEntDetails_relatives_Column_Photo + " FROM " + PatIEntDetails_relatives_table;
        sqliteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawquery(selectAllrelativesPhotos,null);

        // looPing through all rows and adding relatives' photos (all questions) to List
        if(cursor!=null && cursor.getCount() > 0) {
            if (cursor.movetoFirst()) {
                do {
                    PeopleModeQuestions qn = new PeopleModeQuestions();
                    qn.setPhoto(cursor.getString(cursor.getColumnIndex(PatIEntDetails_relatives_Column_Photo)));
                    listofrelativesPhotos.add(qn);
                } while (cursor.movetoNext());
            }
        }
        cursor.close();
        db.close();

        return listofrelativesPhotos;
    }

    // get current relative's photo (current question)
    public PeopleModeQuestions getrelativesPhotoQuestion(int relatives_ID) {
        // Select relative's Photo data from table (questions)
        String selectcurrentrelativesPhoto = "SELECT " + PatIEntDetails_relatives_Column_Photo + " FROM " + PatIEntDetails_relatives_table
                + " WHERE " + PatIEntDetails_relatives_Column_relativesID + " = " + relatives_ID;
        sqliteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawquery(selectcurrentrelativesPhoto,null);
        if(cursor!=null && cursor.getCount() > 0) {
            if (cursor.movetoFirst()) {
                do {
                    PeopleModeQuestions qn = new PeopleModeQuestions();
               
                    qn.setPhoto(cursor.getString(cursor.getColumnIndex(PatIEntDetails_relatives_Column_Photo)));
                    //listofrelativesPhotos.add(qn);
                } while (cursor.movetoNext());
            }
        }
        cursor.close();
        db.close();
        //return selectcurrentrelativesPhoto;
        return null;
    }

    // People Mode Answers - PatIEntDetails_relatives_Relationships
    public ArrayList <PeopleModeAnswers> getrelativesPhotoAnswersByrelativesID(int relatives_ID) {
        ArrayList<PeopleModeAnswers> relationships = new ArrayList<>();
        relationships.clear(); // clear ArrayList to allow new relationships answer data to be added

        // Select relative's Relationship data from table (answers)
        String selectrelativesPhotoAnswersByrelativesID = "SELECT " + PatIEntDetails_relatives_Relationships_Column_Relationship + " FROM " + PatIEntDetails_relatives_Relationships_table
                + " WHERE " + PatIEntDetails_relatives_Relationships_Column_relativesID + " = " + relatives_ID;
        sqliteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawquery(selectrelativesPhotoAnswersByrelativesID,null);

        // looPing through all rows and adding relationships (answers) to ArrayList
        if(cursor!=null && cursor.getCount() > 0) {
            if (cursor.movetoFirst()) {
                do {
                    PeopleModeAnswers answers = new PeopleModeAnswers();
                    answers.setRelationship1(cursor.getString(cursor.getColumnIndex(PatIEntDetails_relatives_Relationships_Column_Relationship)));
                    answers.setRelationship2(cursor.getString(cursor.getColumnIndex(PatIEntDetails_relatives_Relationships_Column_Relationship)));
                    relationships.add(answers);

 
                } while (cursor.movetoNext());
            }
        }
        cursor.close();
        db.close();

        return relationships;
    }


    @OverrIDe
    public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
        if (oldVersion != newVersion) {
            db.execsql("DROP table IF EXISTS " + PatIEnt_Details_table);
            db.execsql("DROP table IF EXISTS " + PatIEntDetails_relatives_table);
            db.execsql("DROP table IF EXISTS " + PatIEntDetails_relatives_Relationships_table);
            // create tables again
            onCreate(db);
        }
    }

    @OverrIDe
    public voID onConfigure(sqliteDatabase db) {
        db.setForeignKeyConstraintsEnabled(true);
        super.onConfigure(db);
    }
}

PeopleModeQuestions 类

2021-04-28 03:28:52.702 5617-5617/com.example.memorydementia E/AndroIDRuntime: FATAL EXCEPTION: main
    Process: com.example.memorydementia,PID: 5617
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memorydementia/com.example.memorydementia.PeopleModeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.memorydementia.PeopleModeQuestions.getPhoto()' on a null object reference
        at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:3449)
        at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at androID.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at androID.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at androID.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at androID.os.Handler.dispatchMessage(Handler.java:106)
        at androID.os.Looper.loop(Looper.java:223)
        at androID.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.androID.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.memorydementia.PeopleModeQuestions.getPhoto()' on a null object reference
        at com.example.memorydementia.PeopleModeActivity.onCreate(PeopleModeActivity.java:97)
        at androID.app.Activity.performCreate(Activity.java:8000)
        at androID.app.Activity.performCreate(Activity.java:7984)
        at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:3422)
        at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at androID.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at androID.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at androID.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at androID.os.Handler.dispatchMessage(Handler.java:106) 
        at androID.os.Looper.loop(Looper.java:223) 
        at androID.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.androID.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的Android Studio:SQLite 某种形式的测验。在 logcat 中出现错误全部内容,希望文章能够帮你解决Android Studio:SQLite 某种形式的测验。在 logcat 中出现错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: