ActiveAndroid Model类未正确初始化

时间:2014-01-02 12:58:25

标签: android android-sqlite activeandroid

我正在尝试使用ActiveAndroid来处理我的应用中的数据库。

这是我的模特课:

package ro.adrian.trivia.datamodel;

import java.io.Serializable;

import ro.adrian.trivia.database.TriviaSQLiteHelper;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

@Table(name = TriviaSQLiteHelper.TABLE_USERS)
public class User extends Model implements Serializable, Comparable<User>{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Column(name = TriviaSQLiteHelper.COLUMN_USER_NAME)
    private String username;
    @Column(name = TriviaSQLiteHelper.COLUMN_FIRST_NAME)
    private String first_name;
    @Column(name = TriviaSQLiteHelper.COLUMN_LAST_NAME)
    private String last_name;
    @Column(name = TriviaSQLiteHelper.COLUMN_PASSWORD)
    private String password;

    public User(){
        super();
        this.username="";
        this.first_name="";
        this.last_name="";
        this.password="";
    }

    public User(String username, String password, String firstName, String lastName){
        super();
        this.username = username;
        this.password = password;
        this.first_name = firstName;
        this.last_name = lastName;
    }


    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString(){
        return this.first_name + " " + this.last_name;
    }


    public String getUsername() {
        return username;
    }


    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public int compareTo(User arg0) {
        return this.username.compareTo(arg0.username);
    }
}

以下是插入新用户的方法:

public User insertUser(String username, String password, String firstName, String lastName){
        User newUser = new User(username, password, firstName, lastName);
        newUser.save();
        return newUser;
    }

这是我的清单:

<!--
  Copyright 2013 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ro.adrian.trivia"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <meta-data
            android:name="AA_DB_NAME"
            android:value="trivia.db" />
        <meta-data
            android:name="AA_DB_VERSION"
            android:value="1" />

        <activity
            android:name="ro.adrian.trivia.activities.HomeActivity"
            android:label="@string/app_name"
            android:theme="@style/noAnimTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="ro.adrian.trivia.activities.MainActivity"
            android:theme="@style/noAnimTheme" >
        </activity>
    </application>

</manifest>

这里我尝试插入一个新用户:

UserDataSource.getInstance().insertUser("adi", "123", "Adrian", "Olar");

这是logcat:

01-02 14:56:08.818: E/AndroidRuntime(10129): FATAL EXCEPTION: main
01-02 14:56:08.818: E/AndroidRuntime(10129): Process: ro.adrian.trivia, PID: 10129
01-02 14:56:08.818: E/AndroidRuntime(10129): java.lang.NullPointerException
01-02 14:56:08.818: E/AndroidRuntime(10129):    at com.activeandroid.Cache.getTableInfo(Cache.java:146)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at com.activeandroid.Model.<init>(Model.java:50)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at ro.adrian.trivia.datamodel.User.<init>(User.java:36)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at ro.adrian.trivia.database.UserDataSource.insertUser(UserDataSource.java:31)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at ro.adrian.trivia.fragments.LoginFragment$1.onClick(LoginFragment.java:31)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at android.view.View.performClick(View.java:4438)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at android.view.View$PerformClick.run(View.java:18422)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at android.os.Handler.handleCallback(Handler.java:733)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at android.os.Looper.loop(Looper.java:136)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at android.app.ActivityThread.main(ActivityThread.java:5017)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at java.lang.reflect.Method.invoke(Method.java:515)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-02 14:56:08.818: E/AndroidRuntime(10129):    at dalvik.system.NativeStart.main(Native Method)

老实说,我不知道我的代码有什么问题...任何建议都非常感谢!

2 个答案:

答案 0 :(得分:10)

我想您忘记在清单中指向ActiveAndroid应用程序类。您的应用程序标记应包含以下内容:

<application android:name="com.activeandroid.app.Application" ...>

Source

答案 1 :(得分:1)

可以创建以下类:

import com.activeandroid.ActiveAndroid;

public class Application extends android.app.Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ActiveAndroid.initialize(this);

    }
}

之后包括进入以下代码:

<application
    android:name=".Application"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher" ...