2013-10-04 39 views
-2

美好的一天。我有下面的小應用程序。每當它遇到螺栓線,應用程序停止。之後我已經註釋掉了這些行,只是爲了測試發生錯誤的位置。我也附上了DataHelper以供您參考。我是eclipse的新手,你能否這樣善待我,看看我做錯了什麼。Eclipse Android應用程序停止

package com.example.*****; 

import android.os.Bundle; 
import android.view.Menu; 
import android.app.Activity; 
import java.util.List; 
import android.util.Log; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    private TextView output; 
    private DataHelper dh; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    this.output=(TextView) this.findViewById(R.id.out_text); 
     **this.dh = new DataHelper(this);** 
    // this.dh.deleteAll(); 
    // this.dh.insert("Porkey pig"); 
    // this.dh.insert("Foghorn"); 
    // List<String> names = this.dh.selectAll(); 
    // StringBuilder sb = new StringBuilder(); 
    // sb.append("Names in database:\n"); 

//   for (String name : names) { 
    //  sb.append(name + "\n"); 
    // } 

    // Log.d("EXAMPLE","names size - " + names.size()); 
    // this.output.setText(sb.toString()); 
    } 

// @Override 
// public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
    // getMenuInflater().inflate(R.menu.main, menu); 
    // return true; 
// } 

} 

DataHelper

package com.example.*****; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteStatement; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
import java.util.ArrayList; 
import java.util.List; 

public class DataHelper { 
    private static final String DATABASE_NAME = "whkgyn.db"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String TABLE_NAME = "attend"; 

    private Context context; 
    private SQLiteDatabase db; 

    private SQLiteStatement insertStmt; 
    private static final String INSERT = "insert into " 
      + TABLE_NAME + "(name) values (?)"; 

    public DataHelper(Context context){ 
     this.context = context; 
     OpenHelper openhelper = new OpenHelper(this.context); 
     this.db = this.db; 

     //this.db = OpenHelper.getWritableDatabase(); 
     this.insertStmt = this.db.compileStatement(INSERT); 
    } 

    public long insert(String name){ 
     this.insertStmt.bindString(1,name); 
     return this.insertStmt.executeInsert(); 
    } 

    public void deleteAll() { 
     this.db.delete(TABLE_NAME, null,null); 
    } 

    public List <String> selectAll(){ 
     List<String> list = new ArrayList<String>(); 
     Cursor cursor = this.db.query(TABLE_NAME, new String[] {"name"}, 
       null, null, null, null,"name desc"); 

     if (cursor.moveToFirst()){ 
      do { 
       list.add(cursor.getString(0)); 
      } while (cursor.moveToNext()); 
     } 

     if (cursor != null && !cursor.isClosed()){ 
      cursor.close(); 
      } 

     return list; 
     } 

    private static class OpenHelper extends SQLiteOpenHelper { 
     OpenHelper(Context context) { 
      super(context, DATABASE_NAME,null,DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db){ 
       db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)"); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
      Log.w("Example","Upgrading database, this will drop tables and recrate"); 
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
      onCreate(db); 
     } 
    } 



    } 
+0

是什麼logcat的說? – Raptor

+0

上帝創造了男人,男人創造了日食,日誌貓使用它。 –

回答

0

我可以知道錯誤的,但我的頭頂部,你可以嘗試一下做:

this.dh = new DataHelper(getApplicationContext()); 
+0

這是我的Logcat.I不理解它在說什麼。 – Slettie

+0

我如何粘貼我的logcat?太長了。 – Slettie

+0

我們不需要整個事情,只需要相關的線路。 –