Question

I am trying to use ActiveAndroid for handling the DB in my app.

Here is my model class:

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);
    }
}

Here is the method to insert a new user:

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

This is my manifest:

<!--
  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>

Here i try to insert a new user:

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

And this is the 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)

I honestly have no idea what could be wrong with my code... any suggestion highly appreciated!

Was it helpful?

Solution

I think you forgot to point to the ActiveAndroid application class in your manifest. Your application tag should contain this:

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

Source

OTHER TIPS

It's possible to create the follow class:

import com.activeandroid.ActiveAndroid;

public class Application extends android.app.Application {

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

    }
}

And after that include into manifest the follow code:

<application
    android:name=".Application"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher" ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top