本帖最后由 zhang_bamboo 于 2011-05-02 17:33:42 编辑

解决方案 »

  1.   

    OpDate类:
    package Comm;import java.sql.SQLException;
    import android.database.Cursor;
    import android.util.Log;
    import DataOperate.Contact;public class OpDate {
    private static Contact opDb;
    private static int AccountID;
    public OpDate()
    {}

    public int GetNewId(String tabName,String idName) throws SQLException
    {
    opDb=new Contact();
    int rtValue;
    Log.v("GetNewId", "1111111111111");
    Log.v("GetNewId", "select max("+idName+") from "+tabName);
    String rt=opDb.selectTopOne("select max("+idName+") from "+tabName);

    Log.v("GetNewId", "222222222222222222-----"+rt);
    if(rt!=null&&!rt.equals(""))
    {
    rtValue=new Integer(rt);
    if(rtValue%2==0)//偶数
    return rtValue+1;
    else
    return rtValue+2;
    }
    return 1;
    }

    public static int getAccountID()
    {
    return AccountID;
    }

    public static void setAccountID()
    {
    opDb=new Contact();
    AccountID=new Integer(opDb.selectTopOne("select top 1 AutoID from T_Login"));
    }
    }Contact类:package DataOperate;import android.app.Activity;
    import android.content.ContentValues;
    import android.os.Bundle;
    import android.util.Log;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;///联系人管理
    public class Contact extends Activity {
    DbHelper dbHelper; public Contact()
    {
    dbHelper = new DbHelper(Contact.this, "ContactPerson.db", 1); 
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 创建DatabaseHelper对象
    dbHelper = new DbHelper(Contact.this, "ContactPerson.db", 1);
    } public long InsertData(String tableName,ContentValues cv)  throws SQLException {
    return dbHelper.insert(tableName, cv);
    } public int UpdateData(String tabName,ContentValues cv,String where,String[] whereValue)
     throws SQLException {
    return dbHelper.update(tabName, cv, where, whereValue);
    } public Cursor QueryData(String tabName, String[] columns, String selection,
    String[] selectionArgs, String groupBy, String having,
    String orderBy) throws SQLException {
    Cursor cursor = dbHelper.select(tabName, columns, selection, selectionArgs, groupBy, having, orderBy);
    return cursor;
    } public int DeleteData(String tabName,String where,String[] whereValue) 
     throws SQLException {
    return dbHelper.delete(tabName, where,whereValue);
    }

    public void execSQL(String sql) throws SQLException {
    dbHelper.execSQL(sql);
    } public String selectTopOne(String strSql)throws SQLException {
    Log.v("Contact", "0000000000000000000");
    return dbHelper.selectTopOne(strSql);
    }
    @Override
    protected void onDestroy() {
    super.onDestroy();
    if (dbHelper != null) {
    dbHelper.close();
    }
    }
    }
      

  2.   

    DbHelper 类:package DataOperate;import Comm.RESManager;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;public class DbHelper extends SQLiteOpenHelper {
    private static final String dbName = "ContactPerson.db";
    private static final int version = 1;
    private Context cont; public DbHelper(Context context, String name, CursorFactory factory,
    int version) {
    // 必须通过super调用父类当中的构造函数
    super(context, name, factory, version);
    cont = context;
    } public DbHelper(Context context, String name, int version) {
    this(context, name, null, version);
    cont = context;
    } public DbHelper(Context context, String name) {
    this(context, name, version);
    cont = context;
    } public DbHelper(Context context) {
    this(context, dbName, version);
    cont = context;
    } @Override
    public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    Activity activity = (Activity) cont;
    String[] sql = RESManager.getSQL(activity);
    try {
    for (int i = 0; i < sql.length; i++) {
    db.execSQL(sql[i]);
    Log.v("onCreate Database", sql[i]);
    }
    } catch (SQLException ex) {
    Log.v("onCreate Database", ex.getMessage());
    }
    } @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    } public Cursor select(String tabName, String[] columns, String selection,
    String[] selectionArgs, String groupBy, String having,
    String orderBy) throws SQLException {
    SQLiteDatabase db = this.getReadableDatabase(); // 调用SQLiteDatabase对象的query方法进行查询,返回一个Cursor对象:由数据库查询返回的结果集对象
    // 第一个参数String:表名 ;如:"user"
    // 第二个参数String[]:要查询的列名,null返回所有列 ;如:new String[] { "id","name" }
    // 第三个参数String:查询条件,null返回所有行;如:"id=?"
    // 第四个参数String[]:查询条件的参数 ;如:new String[] { "1" }
    // 第五个参数String:对查询的结果进行分组,null不分组
    // 第六个参数String:对分组的结果进行限制
    // 第七个参数String:对查询的结果进行排序,传递null将使用默认的排序顺序,这可能是无序的。
    Cursor cursor = db.query(tabName, columns, selection, selectionArgs,
    groupBy, having, orderBy);
    return cursor;
    } public long insert(String tabName, ContentValues cv) throws SQLException {
    // 得到一个可写的SQLiteDatabase对象
    SQLiteDatabase db = this.getWritableDatabase();
    // 调用insert方法,就可以将数据插入到数据库当中
    // 第一个参数:表名称
    // 第二个参数:SQl不允许一个空列,如果ContentValues是空的,那么这一列被明确的指明为NULL值
    // 第三个参数:ContentValues对象
    // 返回值为 在新插入的行的行号,如果发生了错误,返回-1
    long row = db.insert(tabName, null, cv);
    // if (db != null)
    // db.close();
    return row;
    } public int delete(String tabName, String where, String[] whereValue)
    throws SQLException {
    int rtRows;
    SQLiteDatabase db = this.getWritableDatabase();
    // where=where+"=?";
    // 调用SQLiteDatabase对象的delete方法进行删除操作
    // 第一个参数String:表名
    // 第二个参数String:条件语句
    // 第三个参数String[]:条件值
    // 返回收影响的行数???
    rtRows = db.delete(tabName, where, whereValue);
    // if (db != null)
    // db.close();
    return rtRows; } /*
     * public void delete(String tabName,String where1,String where2,String[]
     * whereValue)throws SQLException{ String where=where1+"=? and "+where2;
     * delete(tabName,where,whereValue); }
     */ public int update(String tabName, ContentValues cv, String where,
    String[] whereValue) throws SQLException {
    int rtRows;
    // 得到一个可写的SQLiteDatabase对象
    SQLiteDatabase db = this.getWritableDatabase();
    // where=where+"=?";(外部构造)
    // 调用update方法
    // 第一个参数String:表名
    // 第二个参数ContentValues:ContentValues对象,null将被转化为NULL值插入
    // 第三个参数String:where字句,相当于sql语句where后面的语句,?号是占位符,如果where是一个空值,则更新所有的行
    // 第四个参数String[]:占位符的值
    rtRows = db.update(tabName, cv, where, whereValue);
    // if (db != null)
    // db.close();
    return rtRows;
    } /*
     * public void update(String tabName, ContentValues cv, String where1,String
     * where2, String[] whereValue) { // TODO Auto-generated method stub String
     * where=where1+"=? and "+where2; update(tabName,cv,where,whereValue); }
     */
    public void dropTable(String tabName) throws SQLException {
    String sql = "drop table if exists " + tabName;
    execSQL(sql);
    }
    public String selectTopOne(String strSql)throws SQLException {
    SQLiteDatabase db = this.getWritableDatabase();
    Log.v("Sqlhelper", "1111111111111");
    Cursor c=db.rawQuery(strSql, null);
    Log.v("Sqlhelper", "22222222222222");
    c.moveToFirst();
    Log.v("Sqlhelper", "333333333333333333333");
    String rtValue=c.getString(0);
    Log.v("Sqlhelper", "44444444444444444444"+rtValue);
    c.close();
    return rtValue;
    } public void execSQL(String sql) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(sql);
    // if (db != null)
    // db.close();
    }}
    错误信息:
    05-02 09:24:42.076: VERBOSE/2222(292): 555555555
    05-02 09:24:42.085: VERBOSE/inputContact(292): 000000000000000000
    05-02 09:24:42.164: VERBOSE/inputContact(292): 1111111111111
    05-02 09:24:42.175: VERBOSE/GetNewId(292): 1111111111111
    05-02 09:24:42.185: VERBOSE/GetNewId(292): select max(PersonID) from T_Person
    05-02 09:24:42.194: VERBOSE/Contact(292): 0000000000000000000
    05-02 09:24:42.204: DEBUG/AndroidRuntime(292): Shutting down VM
    05-02 09:24:42.215: WARN/dalvikvm(292): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
    05-02 09:24:42.225: ERROR/AndroidRuntime(292): Uncaught handler: thread main exiting due to uncaught exception
    05-02 09:24:42.255: ERROR/AndroidRuntime(292): java.lang.NullPointerException: println needs a message
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.util.Log.println(Native Method)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.util.Log.v(Log.java:94)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at Base.InputData.inputContact(InputData.java:119)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at Human.Relation.SystemSetup$1.onClick(SystemSetup.java:39)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.View.performClick(View.java:2364)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.View.onTouchEvent(View.java:4179)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.View.dispatchTouchEvent(View.java:3709)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.os.Looper.loop(Looper.java:123)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at android.app.ActivityThread.main(ActivityThread.java:4363)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at java.lang.reflect.Method.invoke(Method.java:521)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    05-02 09:24:42.255: ERROR/AndroidRuntime(292):     at dalvik.system.NativeStart.main(Native Method)
      

  3.   

                Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
                if (cursor.getCount() <= 0) {
                    return trValue = "联系人无资料请添加联系人资料";
                }
    这个地方要先判断cursor != null
      

  4.   

    您只是把EXCEPTION贴出来了,日志到底是记到哪出错了?
      

  5.   

    代码太长了,看的出是执行到DBHelper的selectTopOne的第一句出错,nullpointer是因为你在第一个java文件中输出log信息,e.getMessage()是null,具体它为什么是null我不清楚。楼主多debug
      

  6.   

    单步跟踪下代码,某个try,catch块里面的代码执行错误了,从而产生系统无法识别的异常,因此导致系统产生了异常,但是异常对象为null。
      

  7.   

    谢谢各位,主要是我之前基本没有接触过java,连它的调试环境就不怎么了解,现在老板突然让我给他的手机做个软件,有没有人能讨论 所以很抓狂,各位可有人愿意当我老师 教我一些基本的东西,谢谢