0

我的应用程序出现问题。似乎我无法打开我的应用程序,Activity也无法将数据从 aListView插入数据库。

但是,我可以在一个类中创建 5 个表之前打开Activity并将数据插入数据库。DBAdapter

但是在我创建了一个表之后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'

这是我在其中创建 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();
}

}

我从这两个例子
1. http://www.devdaily.com/android/sqlite-foreign-keys-example
2. 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

从您的代码看来,您的getAllNames()方法没有返回值,这就是您得到NullPointerException. 尝试在课堂上使用您的上下文,BuddyDBAdapter如下所述。

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

尝试这个

于 2012-08-21T07:10:25.360 回答
0

BuddyDBAdapter的班级没有DatabaseHelper像您的基班那样使用班级。在DatabaseHelper该类中,您实际上是在创建和打开数据库。

BuddyDBAdapter中,db尚未初始化并正在引发异常。修改此类以使用在基类中编写的功能。

希望这可以帮助...

于 2012-08-21T06:28:52.430 回答