0

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

清单.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>

主要的.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>

显示.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>

常量.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)

请大家帮帮我,因为这让我发疯了!!!!!!

4

2 回答 2

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。

于 2012-06-24T18:10:29.210 回答
0

你不应该使用

android:maxSdkVersion="17"

因为

  1. SDK 版本 17 不存在,并且

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

因此,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();
    }
});
于 2012-06-24T16:52:43.917 回答