将数据添加到sqlite时的java.lang.NullPointerException

时间:2013-09-28 06:21:37

标签: android nullpointerexception

检查rdio4时,我收到Nullpointerexception错误 代码:

Answerpage.java

    package com.example.psikotestproject;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class AnswerPage extends Activity{

@Override
    protected void onDestroy() {


        super.onDestroy();
    }

DbHelper dbhelper;
SQLiteDatabase db;
Context contextim;


        int i=1;
        int obko=0;
        int soma=0;
        int kadu=0;
        int depr=0;
        int foan=0;

        static float somahesap=0.0f;
        static float obkohesap=0.0f;
        static float kaduhesap=0.0f;
        static float deprhesap=0.0f;
        static float foanhesap=0.0f;


        //dizim
        final String questions[]={
                "Baş ağrısı" ,
                "Sinirlilik ya da içinin titremesi",
                "Zihinden atamadığınız tekrarlayan, hoşa gitmeyen düşünceler",
                "Baygınlık ya da baş dönmesi",
                "Cinsel arzu ve ilginin kaybı",
                "Başkaları tarafından eleştirilme duygusu",
                "Herhangi bir kimsenin düşüncelerinizi kontrol edebileceği

    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.answerpage);

    //component tanımları
    final TextView txtquestion=(TextView)findViewById(R.id.txtquestion);
    final RadioGroup rdiogroup=(RadioGroup)findViewById(R.id.rdiogroup);
    final RadioButton rdio0=(RadioButton)findViewById(R.id.rdio0);
    final RadioButton rdio1=(RadioButton)findViewById(R.id.rdio1);
    final RadioButton rdio2=(RadioButton)findViewById(R.id.rdio2);
    final RadioButton rdio3=(RadioButton)findViewById(R.id.rdio3);
    final RadioButton rdio4=(RadioButton)findViewById(R.id.rdio4);
    final Button btndigersoru=(Button)findViewById(R.id.btndigersoru);
    final Button btnoncekisoru=(Button)findViewById(R.id.btnoncekisoru);

    txtquestion.setText(questions[i-1]);


    //diğersorubutonclick

    btndigersoru.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
if(rdio0.isChecked()||rdio1.isChecked()||rdio2.isChecked()||rdio3.isChecked()||rdio4.isChecked())
{
            txtquestion.setText(questions[i]);


            if(rdio0.isChecked()==true)
            {
                dbhelper.addKayitlar(rdiogroup.getCheckedRadioButtonId());
                //secili[i]=rdiogroup.getCheckedRadioButtonId();
                rdiogroup.clearCheck();


            }

            if(rdio1.isChecked()==true)
            {
                if(i==1||i==4||i==12||i==27||i==40||i==42||i==48||i==52||i==53||i==56||i==58)
                    soma++;
                if(i==3||i==9||i==10||i==28||i==38||i==45||i==46||i==51||i==55||i==65)
                    obko++;
                if(i==6||i==21||i==34||i==36||i==37||i==41||i==61||i==69||i==73)
                    kadu++;
                if(i==4||i==5||i==15||i==20||i==22||i==26||i==29||i==30||i==31||i==32||i==54||i==71||i==79)
                    depr++;
                if(i==13||i==25||i==47||i==50||i==70||i==75||i==82)
                    foan++;

                dbhelper.addKayitlar(rdiogroup.getCheckedRadioButtonId());
                //secili[i]=rdiogroup.getCheckedRadioButtonId();
                rdiogroup.clearCheck();

            }
            if(rdio2.isChecked()==true)
            {
                if(i==1||i==4||i==12||i==27||i==40||i==42||i==48||i==52||i==53||i==56||i==58)
                    soma=soma+2;
                if(i==3||i==9||i==10||i==28||i==38||i==45||i==46||i==51||i==55||i==65)
                    obko=obko+2;
                if(i==6||i==21||i==34||i==36||i==37||i==41||i==61||i==69||i==73)
                    kadu=kadu+2;
                if(i==4||i==5||i==15||i==20||i==22||i==26||i==29||i==30||i==31||i==32||i==54||i==71||i==79)
                    depr=depr+2;
                if(i==13||i==25||i==47||i==50||i==70||i==75||i==82)
                    foan=foan+2;

                dbhelper.addKayitlar(rdiogroup.getCheckedRadioButtonId());
                //secili[i]=rdiogroup.getCheckedRadioButtonId();
                rdiogroup.clearCheck();
            }

            if(rdio3.isChecked()==true)
            {
                if(i==1||i==4||i==12||i==27||i==40||i==42||i==48||i==52||i==53||i==56||i==58)
                    soma=soma+3;
                if(i==3||i==9||i==10||i==28||i==38||i==45||i==46||i==51||i==55||i==65)
                    obko=obko+2;
                if(i==6||i==21||i==34||i==36||i==37||i==41||i==61||i==69||i==73)
                    kadu=kadu+2;
                if(i==4||i==5||i==15||i==20||i==22||i==26||i==29||i==30||i==31||i==32||i==54||i==71||i==79)
                    depr=depr+2;
                if(i==13||i==25||i==47||i==50||i==70||i==75||i==82)
                    foan=foan+3;

                dbhelper.addKayitlar(rdiogroup.getCheckedRadioButtonId());
                //secili[i]=rdiogroup.getCheckedRadioButtonId();
                rdiogroup.clearCheck();

            }
            if(rdio4.isChecked()==true)
            {
                if(i==1||i==4||i==12||i==27||i==40||i==42||i==48||i==52||i==53||i==56||i==58)
                    soma=soma+4;
                if(i==3||i==9||i==10||i==28||i==38||i==45||i==46||i==51||i==55||i==65)
                    obko=obko+4;
                if(i==6||i==21||i==34||i==36||i==37||i==41||i==61||i==69||i==73)
                    kadu=kadu+4;
                if(i==4||i==5||i==15||i==20||i==22||i==26||i==29||i==30||i==31||i==32||i==54||i==71||i==79)
                    depr=depr+4;
                if(i==13||i==25||i==47||i==50||i==70||i==75||i==82)
                    foan=foan+4;



                dbhelper.addKayitlar(rdiogroup.getCheckedRadioButtonId());

                //secili[i]=rdiogroup.getCheckedRadioButtonId();
                rdiogroup.clearCheck();
            }


        }

        if(i<16)
            i++;
        else{}

            if(i>1)
            {
                btnoncekisoru.setVisibility(View.VISIBLE);

            }


                if(i>=9)
            //if(i>=90)
            {   
                //En son hesaplar   
                somahesap=(float) (soma/11.0);
                obkohesap=(float) (obko/10.0);
                kaduhesap=(float) (kadu/9.0);
                deprhesap=(float) (depr/13.0);
                foanhesap=(float) (foan/7.0);

                Intent finishingActivity=new Intent("com.example.psikotestproject.RESULTPAGE");
                startActivity(finishingActivity);
            }



        }

    });

    //btnoncekibutonclick

    btnoncekisoru.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){

            rdiogroup.clearCheck();
            dbhelper.getKayitlar(i-1);
            txtquestion.setText(questions[i-1]);


            i--;

            //rdiogroup.check(secili[i]);

            if(i<=0)
            {
                btnoncekisoru.setVisibility(View.INVISIBLE);    

            }

        }

    }); 


}

Dbhelper.java

public class DbHelper extends SQLiteOpenHelper {

    // Tüm Static değişkenler
    // Database Versiyonu
    private static final int DATABASE_VERSION = 1;

    // Database Adı
    private static final String DATABASE_NAME = "dbSecimler";

    // Kayitlar Tablosunun Adı
    private static final String TABLE_NAME = "Secimler";

    // Kayitlar Tablosunun Kolon Adları
    private static final String KEY_CEVAP = "Cevaplar";

    // Yapılandırıcı metod
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Database Oluşturma işlemi.
    @Override
    public void onCreate(SQLiteDatabase db) {
        String create_table = "CREATE TABLE " + TABLE_NAME + "(id INTEGER PRİMARY KEY AUTOINCREMENT" 
                + KEY_CEVAP + " INTEGER," + ")";

        db.execSQL(create_table);
    }

    // Database Yükseltme işlemi.
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Varsa şayet eski tabloyu sil.
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        // Tekrar tablo oluştur.
        onCreate(db);
    }


// Yeni Kayıt Eklemek.
void addKayitlar(int i) {
    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_CEVAP, i);

    // Ekleme işlemi...
    db.insert(TABLE_NAME, null , values);
    db.close(); // Açık olan database i kapat.
    }



//İstenilen Kaydı Getirmek.
Kayitlar getKayitlar(int i) {
 SQLiteDatabase db = this.getReadableDatabase();

 Cursor cursor = db.query(TABLE_NAME, new String[] {KEY_CEVAP},null, null, null, null, null);
 if (cursor != null)
     cursor.moveToFirst();

 Kayitlar kayit = new Kayitlar(Integer.parseInt(cursor.getString(1)));
 // return Kayitlar
 return kayit;
}

}

LogCat上的错误

threadid=1: thread exiting with uncaught exception (group=0x4001d800)
FATAL EXCEPTION: main
java.lang.NullPointerExceptionat com.example.psikotestproject.AnswerPage$1.onClick(AnswerPage.java:282)
-
-
-

和282是:dbhelper.addKayitlar(rdiogroup.getCheckedRadioButtonId()); rdiogroup.getCheckedRadioButtonId()not null我很确定。我想我有一个关于保存数据库的错误。我可以解决什么?

1 个答案:

答案 0 :(得分:0)

对于您已实施findViewById()的视图,您似乎缺少onClickListener