使用SQLIte时出现未知错误

时间:2012-06-24 16:49:40

标签: android sqlite contacts

我是android开发的新手,这是我第一次尝试使用SQLite数据库。我正面临着一个我无法理解的错误,所以如果可以的话,请让我帮忙。我的应用程序有一个文本视图和3个按钮,一个用于读取和显示移动设备的收缩,一个用于显示数据库记录,另一个用于将联系人保存到数据库。 下面附带的代码是main.java,它从其他文件中获取常量。

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="medialab.elearning"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:maxSdkVersion="17"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".DbProjectActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>

 </manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:layout_width="fill_parent"  
android:layout_height="fill_parent"
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<TextView android:id="@+id/textview"    
android:layout_width="fill_parent"android:layout_height="wrap_content"/>

<Button
android:id="@+id/btn2"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="50dip"
android:layout_marginTop="75dip"
android:text="@+string/viewDb" />

<Button
android:id="@+id/btn"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="50dip"
android:text="@+string/name" />

<Button
android:id="@+id/btn3"
android:layout_width="250dip"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="50dip"
android:text="@+string/viewContacts" />

</LinearLayout>

show.xml

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView android:layout_width="fill_parent"  
android:layout_height="fill_parent"
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<TextView android:id="@+id/showView" android:layout_width="fill_parent"        
android:layout_height="wrap_content"/>

</ScrollView>

Constants.java

public interface Constants extends BaseColumns {
    public static final String TABLE_NAME= "contact";
    public static final String CONTACT_NAME= "ContactName";
    public static final String KEY_ID="_id";
}

ContactsData.java

public class ContactsData extends SQLiteOpenHelper {

public static final String DATABASE_NAME="contacts.db";
private static final int DATABASE_VERSION=1;


public ContactsData(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT,   
ContactName TEXT NOT NULL);");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
onCreate(db);
}

}

DbProjectActivity.java

package medialab.elearning;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.content.ContentResolver;   
import android.net.Uri;
import android.provider.ContactsContract;   
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import static medialab.elearning.Constants.TABLE_NAME;   
import static medialab.elearning.Constants.CONTACT_NAME;
import static medialab.elearning.ContactsData.DATABASE_NAME;
import static medialab.elearning.Constants.KEY_ID;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

 public class DbProjectActivity extends Activity {
/** Called when the activity is first created. */
private static final String DNAME= ContactsContract.Contacts.DISPLAY_NAME;
private static final Uri URI= ContactsContract.Contacts.CONTENT_URI;
public String name="";
public String tableName=Constants.TABLE_NAME;
public String contactName=Constants.CONTACT_NAME;
public String contactname="ContactName";
public Integer savedCounter=0, addedCounter=0;
private ContactsData con;
Long rawId=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tv = (TextView) findViewById(R.id.textview);
    tv.setText("");
    con = new ContactsData(this);
    try{
        Button button1 = (Button) findViewById(R.id.btn2);
        button1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                showRecords();
            }
        });

        Button button2 = (Button) findViewById(R.id.btn3);
        button2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                showContacts();
            }
        });

        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                TextView tv = (TextView) findViewById(R.id.textview);
                tv.setText("");
                Bundle extras= getIntent().getExtras();
                rawId = extras.getLong(Constants.KEY_ID);
                ContentResolver cr= getContentResolver();
                Cursor cu= cr.query(URI, null, null, null, null);
                if(cu.getCount()>0){
                    cu.moveToFirst();}
                while(cu.moveToNext()){
                    name=cu.getString(cu.getColumnIndex(DNAME));
                    if(rawId!=null){
                        search(name);
                        rawId+=1;
                    }
                    else{
                        addContact(name);
                        rawId+=1;
                    }
                }
                tv.setText("CONTACTS FOUND IN YOUR MOBILE: " + (addedCounter + savedCounter) + "\nALLREADY STORED IN DATABASE: " + savedCounter + "\nNEW CONTACTS ADDED IN DATABASE: " + addedCounter);
                addedCounter=0;
                savedCounter=0;
                cu.close();
            }
        });
    }
    finally {
        con.close();
    }
}

@Override
public void onPause(){
    super.onPause();
    con.close();
}

@Override
public void onResume(){
    super.onResume();
    con = new ContactsData(this);
    try{
        Button button1 = (Button) findViewById(R.id.btn2);
        button1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                showRecords();
            }
        });
        Button button2 = (Button) findViewById(R.id.btn3);
        button2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                showContacts();
            }
        });
        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view){
                TextView tv = (TextView) findViewById(R.id.textview);
                tv.setText("");
                Bundle extras= getIntent().getExtras();
                rawId = extras.getLong(Constants.KEY_ID);
                ContentResolver cr= getContentResolver();
                Cursor cu= cr.query(URI, null, null, null, null);
                if(cu.getCount()>0){    
                    cu.moveToFirst();}
                while(cu.moveToNext()){
                    name=cu.getString(cu.getColumnIndex(DNAME));
                    if(rawId!=null){
                        search(name);
                        rawId+=1;
                    }
                    else{
                        addContact(name);
                    }
                }
                tv.setText("CONTACTS FOUND IN YOUR MOBILE: " + (addedCounter + savedCounter) + "\nALLREADY STORED IN DATABASE: " + savedCounter + "\nNEW CONTACTS ADDED IN DATABASE: " + addedCounter);
                addedCounter=0;
                savedCounter=0;
                cu.close();
            }
        });
    } finally{
        con.close();
    }
}


private void search(String name){
    Integer count=0;
    SQLiteDatabase db = con.getReadableDatabase();
    Cursor c = db.rawQuery("select count(*) from contact where " + contactName + "=" + name, null); 
    c.moveToFirst(); 
    count = c.getCount();
    if(count==0){
        addContact(name);
        addedCounter+=1;
    }
    else{
        savedCounter+=1;
    }
    c.close();
    db.close();
}


private void addContact(String name){
    SQLiteDatabase db = con.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Constants.CONTACT_NAME, name);
    db.insertOrThrow(Constants.TABLE_NAME, null, values);
    db.close();
}


private void showRecords(){
    setContentView(R.layout.show);
    StringBuilder builder = new StringBuilder("Saved records:\n");
    SQLiteDatabase db = con.getReadableDatabase();
    Cursor c=db.rawQuery("SELECT * FROM " + Constants.TABLE_NAME, null);
    c.moveToFirst();
    while(!c.isAfterLast())
    {
        long id= c.getLong(0);
        String recordName = c.getString(1);
        builder.append(id).append(": ");
        builder.append(recordName).append("\n");
    }
    TextView text = (TextView) findViewById(R.id.showView);
    text.setText(builder);
}

private void showContacts(){
    Integer contactNum=0;
    String contactName="";
    String contactInfo="";
    String contactsList="";
    setContentView(R.layout.show);
    TextView text = (TextView) findViewById(R.id.showView);
    ContentResolver cr= getContentResolver();
    Cursor cu= cr.query(URI, null, null, null, null);
    if(cu.getCount()>0){    
        while(cu.moveToNext()){
            contactName=cu.getString(cu.getColumnIndex(DNAME));
            contactInfo = contactNum + " " + contactName + "\n";    
            contactsList+= contactInfo;
            contactNum+=1;
        }
    }
    text.setText(contactsList);
}


}

显示联系人和数据库记录的按钮工作正常。 但每当我测试保存按钮时,我在日志文件上得到一个错误 无法理解。 这是日志文件的结果

06-24 20:28:16.233: D/AndroidRuntime(281): Shutting down VM 06-24 20:28:16.233: W/dalvikvm(281): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
06-24   20:28:16.243: E/AndroidRuntime(281): FATAL EXCEPTION: main 06-24 20:28:16.243: E/AndroidRuntime(281): java.lang.NullPointerException 
06-24 20:28:16.243: E/AndroidRuntime(281):  at medialab.elearning.DbProjectActivity$6.onClick(DbProjectActivity.java:122) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at android.view.View.performClick(View.java:2408) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at android.view.View$PerformClick.run(View.java:8816) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at android.os.Handler.handleCallback(Handler.java:587) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at android.os.Handler.dispatchMessage(Handler.java:92) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at android.os.Looper.loop(Looper.java:123) 06-24 20:28:16.243: E/AndroidRuntime(281):   at android.app.ActivityThread.main(ActivityThread.java:4627) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
06-24 20:28:16.243: E/AndroidRuntime(281):  at dalvik.system.NativeStart.main(Native Method)

请各位帮助我,因为它让我疯了!!!!!

2 个答案:

答案 0 :(得分:0)

你不应该使用

android:maxSdkVersion="17"

,因为

  1. SDK版本17不存在,

  2. 99%的时间绝对没有理由阻止更高版本的SDK使用您的应用程序。发布更新的SDK版本时考虑了向后兼容性。

  3. 所以只需删除maxSdkVersion属性,如果仍然出现错误,请发布logcat输出。


    你问“缩进”是什么意思......所以你走了:

    代码不良的代码示例:

    Button button2 = (Button) findViewById(R.id.btn3);
    button2.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View view){
    showContacts();
    }
    });
    

    正确缩进代码的示例

    Button button2 = (Button) findViewById(R.id.btn3);
    button2.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View view){
            showContacts();
        }
    });
    

答案 1 :(得分:0)

你有:

private void search(String name){
    SQLiteDatabase db = con.getReadableDatabase();
    Cursor c = db.rawQuery("select count(*) from contact where " + contactName + "=" + name, null); 
    c.moveToFirst(); 
    int count = c.getInt(0); 
    if(count==0){
        addContact(name);
        addedCounter+=1;
    }
    else{
        savedCounter+=1;
}
    c.close();
    db.close();
}

但是不是c.getInt(0)而是尝试c.getcount()。据我所知,SQLite中没有第0列。 :) 我注意到的其他一些事情是:

private void addContact(String name){
    SQLiteDatabase db = con.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Constants.CONTACT_NAME, name);
    db.insertOrThrow(Constants.TABLE_NAME, null, values);
    db.close();
}

您可能需要将Constants.CONTACT_NAME更改为DNAME。