0

アプリケーションに問題があります。データベースを開くことができないActivityだけでなく、データベースにデータを挿入できないようListViewです。

ただし、 1つのクラスActivityに5つのテーブルを作成する前に、データを開いてデータベースに挿入することはできます。DBAdapter

しかし、1つのテーブルを作成した後DBAdapter、アプリケーションを機能させるのに問題があります。私は自分のアプリケーションを機能させる例に従いました。

これは私のLogCatです:

08-21 13:36:27.421: D/AndroidRuntime(543): Shutting down VM
08-21 13:36:27.421: W/dalvikvm(543): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-21 13:36:27.441: E/AndroidRuntime(543): FATAL EXCEPTION: main
08-21 13:36:27.441: E/AndroidRuntime(543): java.lang.RuntimeException: Unable to start activity ComponentInfo{main.page/main.page.PersonalInformation}: java.lang.NullPointerException
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.os.Looper.loop(Looper.java:137)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.ActivityThread.main(ActivityThread.java:4424)
08-21 13:36:27.441: E/AndroidRuntime(543):  at java.lang.reflect.Method.invokeNative(Native Method)
08-21 13:36:27.441: E/AndroidRuntime(543):  at java.lang.reflect.Method.invoke(Method.java:511)
08-21 13:36:27.441: E/AndroidRuntime(543):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-21 13:36:27.441: E/AndroidRuntime(543):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-21 13:36:27.441: E/AndroidRuntime(543):  at dalvik.system.NativeStart.main(Native Method)
08-21 13:36:27.441: E/AndroidRuntime(543): Caused by: java.lang.NullPointerException
08-21 13:36:27.441: E/AndroidRuntime(543):  at main.page.BuddyDBAdapter.getAllNames(BuddyDBAdapter.java:37)
08-21 13:36:27.441: E/AndroidRuntime(543):  at main.page.PersonalInformation.onCreate(PersonalInformation.java:41)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.Activity.performCreate(Activity.java:4465)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-21 13:36:27.441: E/AndroidRuntime(543):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-21 13:36:27.441: E/AndroidRuntime(543):  ... 11 more
08-21 13:36:27.711: I/dalvikvm(543): threadid=3: reacting to signal 3
08-21 13:36:27.751: I/dalvikvm(543): Wrote stack traces to '/data/anr/traces.txt'
08-21 13:36:28.061: I/dalvikvm(543): threadid=3: reacting to signal 3
08-21 13:36:28.061: I/dalvikvm(543): Wrote stack traces to '/data/anr/traces.txt'

これは私が1つの中に5つのテーブルを作成したコードですDBAdapter

 package main.page;


import android.content.Context;
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 AnniversaryDBAdapter
{

    private static final String DATABASE_NAME = "AllTables";
    private static final int DATABASE_VERSION = 2;

     private static final String CREATE_TABLE_TITLE = "create table titles(title_id integer primary key autoincrement, name text not null, image text not null);";
    private static final String CREATE_TABLE_BUDDIESLIST = "create table buddiesList(name_id integer primary key autoincrement, name text not null);";
    private static final String CREATE_TABLE_LIKES = "create table likes(likes_id integer primary key autoincrement,like text not null, name text not null, FOREIGN KEY(name)REFERENCES "+CREATE_TABLE_BUDDIESLIST+" (name));";
    private static final String CREATE_TABLE_DISLIKES = "create table dislike(dlike_id integer primary key autoincrement, dislike text not null, name text not null, FOREIGN KEY(name)REFERENCES "+CREATE_TABLE_BUDDIESLIST+" (name));";
    private static final String CREATE_TABLE_EVENTS = "create table events(date_id integer primary key autoincrement, date text not null, starttime text not null, endtime text not null, name text not null,title_id integer, desc text not null, alarm text not null, location text not null, FOREIGN KEY(name)REFERENCES "+CREATE_TABLE_BUDDIESLIST+" (name), FOREIGN KEY(title_id)REFERENCES "+CREATE_TABLE_TITLE+" (title_id));";

    private final Context context;
    private static final String TAG = "DBAdapter";

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public AnniversaryDBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }




private static class DatabaseHelper extends SQLiteOpenHelper
{

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

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_TABLE_BUDDIESLIST);
        db.execSQL(CREATE_TABLE_LIKES);
        db.execSQL(CREATE_TABLE_EVENTS);
        db.execSQL(CREATE_TABLE_TITLE);
        db.execSQL(CREATE_TABLE_DISLIKES);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");

        onCreate(db);

    }

}


public AnniversaryDBAdapter open() throws SQLException
{
    this.db = this.DBHelper.getWritableDatabase();
    return this;
}

public void close()
{
    this.DBHelper.close();
}

}

これらの2つの例からフォローしました
1.http : //www.devdaily.com/android/sqlite-foreign-keys-example2
. http://androidforbeginners.blogspot.sg/2010/01/creating-multiple-sqlite -database.html

これは、アプリケーションで開くことができないコードです。

package main.page;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class PersonalInformation extends Activity
{
    Button btnSave, btnBack, btnView;
    EditText txtDate, txtType, txtLikes, txtDislikes;

    final Context context = this;           
    private int mSpinnerNamesId;            

    LikesDBAdapter likeDB = new LikesDBAdapter(this);
    DislikesDBAdapter dislikeDB = new DislikesDBAdapter(this);


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


        BuddyDBAdapter buddyDB = new BuddyDBAdapter(this);

        buddyDB.open();

        Spinner namesSpinner = (Spinner) findViewById(R.id.nameSpinner);
        Cursor namesCursor = buddyDB.getAllNames();
        startManagingCursor(namesCursor);

        String[] from = new String[] {BuddyDBAdapter.KEY_NAME};
        int[] to = new int[] {android.R.id.text1};

        SimpleCursorAdapter namesSpinnerAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, namesCursor, from, to);
        namesSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        namesSpinner.setAdapter(namesSpinnerAdapter);
        buddyDB.close();

        namesSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
            {
                Cursor c = (Cursor)parent.getItemAtPosition(pos);
                mSpinnerNamesId = c.getInt(c.getColumnIndexOrThrow(BuddyDBAdapter.KEY_ROWID));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {               
            }
        });


        btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                likeDB.open();
                long likes_id;
                Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
                String NameValue = nameSpinner.getSelectedItem().toString();

                EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
                String LikesValue = txtLikes.getText().toString();

                likes_id = likeDB.insertLikes(NameValue, LikesValue);
                likeDB.close();

                dislikeDB.open();
                long dislikes_id;
                Spinner names = (Spinner) findViewById(R.id.nameSpinner);
                String NamesValue = names.getSelectedItem().toString();

                EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
                String DislikesValue = txtDislikes.getText().toString();

                dislikes_id = dislikeDB.insertDislikes(NamesValue, DislikesValue);

                Toast.makeText(getBaseContext(), 
                        "Your information is saved successfully!", Toast.LENGTH_SHORT).show();
                dislikeDB.close();
            }
        });

        btnBack = (Button) findViewById(R.id.btnMain);
        btnBack.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                finish();
            }
        });

    }
}

これは、PersonalInformation.javaに接続するBuddyDBAdapter.javaです。

package main.page;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class BuddyDBAdapter extends AnniversaryDBAdapter
{
    public static final String KEY_ROWID = "name_id";
    public static final String KEY_NAME = "name";
    private static final String TAG = "DBAdapter";
    private static final String CREATE_TABLE_BUDDIESLIST = "buddiesList";

    private SQLiteDatabase db;

    public BuddyDBAdapter(Context ctx)
    {
        super(ctx);
    }

    public long insertNames(String name)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        return db.insert(CREATE_TABLE_BUDDIESLIST, null, initialValues);
    }

    public boolean deleteName(long rowId)
    {
        return db.delete(CREATE_TABLE_BUDDIESLIST, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public Cursor getAllNames()
    {
        return db.query(CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
    }

    public Cursor getName(long rowId) throws SQLException
    {
        Cursor c = db.query(true, CREATE_TABLE_BUDDIESLIST, new String[] { KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if(c != null)
        {
            c.moveToFirst();
        }
        return c;
    }
}

どんな助けでもありがたいです。

4

2 に答える 2

0

あなたのクラスはあなたの基本クラスのようにクラスBuddyDBAdapterを使用していません。DatabaseHelperそのDatabaseHelperクラスでは、実際にデータベースを作成して開いています。

BuddyDBAdapterdbは初期化されておらず、例外をスローしています。基本クラスで記述された機能を使用するようにこのクラスを変更します。

お役に立てれば...

于 2012-08-21T06:28:52.430 に答える
0

あなたのコードから、あなたのgetAllNames()メソッドは値を返さないようです。それがあなたが得ている理由ですNullPointerExceptionBuddyDBAdapter以下に述べるように、クラスでコンテキストを使用してみてください。

   private static Context context;
   public BuddyDBAdapter(Context ctx)
{
    this.context = ctx;
}

これを試して

于 2012-08-21T07:10:25.360 に答える