应用程序因空数据库而崩溃

时间:2015-05-28 13:14:45

标签: android sqlite

  

当我按下btn应用程序时崩溃因为数据库中没有数据。

   public void viewBtn(View v){


   try {

    Intent  i2 = new Intent(this,view.class);


   startActivityForResult(i2,500);


   overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); 

} catch (Exception e) {


    Toast.makeText(this,"Add Some Course First ",Toast.LENGTH_LONG).show();
}
  

View.class   这个类是显示来自db的数据。当我的数据库是空的时,我正在显示这个页面应用程序是crshing

       public class view extends Activity  {
        final Context context = this;
    ListView l;
    String m;
   int pos;
   public  String s,kk;
   TextView resultShow;

 public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.view);
resultShow=(TextView) findViewById(R.id.text1);
 l = (ListView) findViewById(R.id.list);
   l.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,readRecords()));






 }

 ArrayList<String> readRecords(){
     ArrayList<String> record = new ArrayList<String>(); 


     SQLiteDatabase database = openOrCreateDatabase("newDb",MODE_PRIVATE, null);
        String sql = "SELECT * from newTabb;";
        Cursor c = database.rawQuery(sql, null);
        double ss;
        double sumOfGr=0;
        int sumOfCr=0;
        while (c.moveToNext()){

            String i ;

            String semester = c.getString(c.getColumnIndex("Semester"));

            String course = c.getString(c.getColumnIndex("Course"));
            double grade = c.getDouble(c.getColumnIndex("Grade"));
            int credit=c.getInt(c.getColumnIndex("Credit"));

            ss=credit*grade;

            sumOfCr=sumOfCr+credit;
            sumOfGr=sumOfGr+ss;

            i = "Semester: "+semester+"  Course: "+course+"  Grade: "+grade;

            m = "Semester:"+semester+"\nCourse: "+course+"\nGrade: "+grade+"\nCreadit: "+credit;

            record.add(i);




        }

        double cgpa=sumOfGr/sumOfCr;

        String.format("%.2f", cgpa);


        //s="CGPA : "+cgpa;



  resultShow.setText(new DecimalFormat("##.##").format(cgpa));


 l.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> a, View v, int i,
            long r) {

         pos=i;

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);


            alertDialogBuilder.setTitle("Details");


            alertDialogBuilder
                    .setMessage(m)
                    .setCancelable(false)
                    .setPositiveButton("Back",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {

                                }

                            });


            AlertDialog alertDialog = alertDialogBuilder.create();


            alertDialog.show();


        }


 });
return record;



 }



 public  String getCgpa(){

     return s;
 }

}

enter image description here

3 个答案:

答案 0 :(得分:2)

查看您的logcat。你会在 logcat 中找到一行错误,如“由:”引起。这里说你没有创建像'newTabb'这样的表。

请添加以下这一行:

database.execSQL("CREATE TABLE IF NOT EXISTS newTabb(Semester VARCHAR,Course VARCHAR,Grade REAL,Credit INTEGER);");

这一行之后:

 SQLiteDatabase database = openOrCreateDatabase("newDb",MODE_PRIVATE, null);

答案 1 :(得分:1)

从设备中卸载应用程序并再次运行。何时在数据库中添加新表。简单的方法是卸载应用程序并再次安装以进行测试。由于设备保留了您的旧数据库。在大多数情况下,它不会添加新表。

答案 2 :(得分:0)

从playstore安装asqlitemanager加载你的数据库以检查它的完整性。每当我修改表时,我都会得到同样的错误,只需擦除应该执行此操作的应用程序的数据。