私は 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>
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>
定数.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)
それは私を狂わせているので、みんな私を助けてください!!!!!