0

在我的应用程序中,我想显示来自 sqlite 的数据。当我按下按钮显示信息时,我想将返回函数 getInfo 的记录放在 textView 中。这是我的代码:

btnShow = (Button) findViewById(R.id.btnInfo);
        final TextView txtNume = (TextView) findViewById(R.id.txtNume);
        final TextView txtPrenume = (TextView) findViewById(R.id.txtPrenume);
        final TextView txtTelefon = (TextView) findViewById(R.id.txtTelefon);
        final TextView txtAdresa = (TextView) findViewById(R.id.txtadresa);
        final TextView txtAdresaEmail = (TextView) findViewById(R.id.txtEmail);
        final TextView txtDataNasterii = (TextView) findViewById(R.id.txtDataNasterii);
        final TextView txtSex = (TextView) findViewById(R.id.txtSex);
        btnShow.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Pacienti pacienti = pacientiDB.getInfo(text);
                if (pacienti != null) {
                    pacienti.setTelefon(txtTelefon.getText().toString());
                    pacienti.setPrenume(txtPrenume.getText().toString());
                    pacienti.setNume(txtNume.getText().toString());
                    pacienti.setAdresa(txtAdresa.getText().toString());
                    pacienti.setDataNasterii(txtDataNasterii.getText().toString());
                    pacienti.setEmail(txtAdresaEmail.getText().toString());
                    pacienti.setSex(txtSex.getText().toString());
                    }}

});

这是函数getInfo:

public Pacienti getInfo(String text) {
        Pacienti returnPacient = null;
        Cursor cursor = null;

        try {

            cursor = database.rawQuery("SELECT * FROM " + PACIENTI_NAME
                    + " WHERE prenume = '" + text + "'", null);
            if (cursor.getCount() > 0) {
                int indexTelefon = cursor.getColumnIndex("telefon");
                int indexPrenume = cursor.getColumnIndex("prenume");
                int indexNume = cursor.getColumnIndex("nume");
                int indexAdresaEmail = cursor.getColumnIndex("adresaEmail");
                int indexSex = cursor.getColumnIndex("Sex");
                int indexAdresaBuletin = cursor.getColumnIndex("AdresaBuletin");
                int indexDatanasterii = cursor.getColumnIndex("dataNasterii");
                cursor.moveToFirst();
                do {
                    String nume = cursor.getString(indexNume);
                    String prenume = cursor.getString(indexPrenume);
                    String Telefon = cursor.getString(indexTelefon);
                    String AdresaEmail = cursor.getString(indexAdresaEmail);
                    String Sex = cursor.getString(indexSex);
                    String AdresaBuletin = cursor.getString(indexAdresaBuletin);
                    String DataNasterii = cursor.getString(indexDatanasterii);

                    returnPacient = new Pacienti();
                    returnPacient.setNume(nume);
                    returnPacient.setPrenume(prenume);
                    returnPacient.setTelefon(Telefon);
                    returnPacient.setAdresa(AdresaBuletin);
                    returnPacient.setEmail(AdresaEmail);
                    returnPacient.setDataNasterii(DataNasterii);
                    returnPacient.setSex(Sex);
                    cursor.moveToNext();

                } while (!cursor.isAfterLast());
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        return returnPacient;
    }

但它没有用。我收到此错误,但我不明白为什么,因为我确认我在数据库中有数据:

04-14 10:35:33.478: E/AndroidRuntime(278): FATAL EXCEPTION: main
04-14 10:35:33.478: E/AndroidRuntime(278): java.lang.NullPointerException
04-14 10:35:33.478: E/AndroidRuntime(278):  at com.example.medicaldoi.ShowInfo$1.onClick(ShowInfo.java:53)
04-14 10:35:33.478: E/AndroidRuntime(278):  at android.view.View.performClick(View.java:2408)
04-14 10:35:33.478: E/AndroidRuntime(278):  at android.view.View$PerformClick.run(View.java:8816)
04-14 10:35:33.478: E/AndroidRuntime(278):  at android.os.Handler.handleCallback(Handler.java:587)
04-14 10:35:33.478: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 10:35:33.478: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
04-14 10:35:33.478: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 10:35:33.478: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 10:35:33.478: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
04-14 10:35:33.478: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-14 10:35:33.478: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-14 10:35:33.478: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)

有人能帮助我吗!

4

1 回答 1

0

您是否使用断点检查过哪些变量为空?此代码中的第 53 行在哪里?

您在哪里实例化您在函数 getInfo 中使用的变量“文本”?你确定这个变量不为空吗?

于 2013-04-14T11:22:15.430 回答