ActiveAndroid icon indicating copy to clipboard operation
ActiveAndroid copied to clipboard

How could i use ActiveAndroid in one class whitch contain a arraylist?

Open rayboot opened this issue 13 years ago • 1 comments

I read the Relationships section,but it's not work. I think there is something wrong with my usage. Anyone could help me?

There are two class.

Originally code like

Class RoundInfo :

public class RoundInfo{

String roundNum;
List<MatchInfo> matchInfos = new ArrayList<MatchInfo>();

public RoundInfo() {
    super();
    // TODO Auto-generated constructor stub
}

public RoundInfo(String roundNum) {
    super();
    this.roundNum = roundNum;
    matchInfos.add(new MatchInfo("11", "21", "1121"));
}

}

Class MatchInfo:

public class MatchInfo{

String home;
String away;
String sorce;

public MatchInfo() {
    super();
}

public MatchInfo(String home, String away, String sorce) {
    super();
    this.home = home;
    this.away = away;
    this.sorce = sorce;
}

}

Then i wanna use ActiveAndroid save roundinfo data to db. And make some change like 1.Let my application class extends com.activeandroid.app.Application 2.Add AA_DB_NAME & AA_DB_VERSION to AndroidManifest.xml 3.Modify class MatchInfo & RoundInfo to

Class RoundInfo:

@Table(name = "RoundInfo") public class RoundInfo extends Model {

@Column(name = "roundNum")
String roundNum;

public List<MatchInfo> matchinfos() {
    return getMany(MatchInfo.class, "MatchInfo");
}

public RoundInfo() {
    super();
    // TODO Auto-generated constructor stub
}

public RoundInfo(String roundNum) {
    super();
    this.roundNum = roundNum;
    matchinfos().add(new MatchInfo("11", "21", "1121"));
    matchinfos().add(new MatchInfo("12", "22", "1222"));
    matchinfos().add(new MatchInfo("13", "23", "1323"));
    matchinfos().add(new MatchInfo("14", "24", "1424"));
}

}

Class MatchInfo:

@Table(name = "MatchInfos") public class MatchInfo extends Model {

@Column(name = "home")
String home;
@Column(name = "away")
String away;
@Column(name = "sorce")
String sorce;

public MatchInfo() {
    super();
    // TODO Auto-generated constructor stub
}

public MatchInfo(String home, String away, String sorce) {
    super();
    this.home = home;
    this.away = away;
    this.sorce = sorce;
}

}

Question: When i new the RoundInfo class and save() it use

    RoundInfo roundInfo1 = new RoundInfo("1");
    roundInfo1.save();

Finally get some errors like

04-01 22:33:37.875: E/AndroidRuntime(14712): FATAL EXCEPTION: main 04-01 22:33:37.875: E/AndroidRuntime(14712): java.lang.NullPointerException 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.activeandroid.query.From.getArguments(From.java:207) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.activeandroid.query.From.execute(From.java:183) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.activeandroid.Model.getMany(Model.java:247) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.example.testdb.RoundInfo.matchinfos(RoundInfo.java:16) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.example.testdb.RoundInfo.(RoundInfo.java:27) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.example.testdb.MainActivity.initDB(MainActivity.java:45) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.example.testdb.MainActivity.access$0(MainActivity.java:43) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.example.testdb.MainActivity$1.onClick(MainActivity.java:36) 04-01 22:33:37.875: E/AndroidRuntime(14712): at android.view.View.performClick(View.java:2486) 04-01 22:33:37.875: E/AndroidRuntime(14712): at android.view.View$PerformClick.run(View.java:9130) 04-01 22:33:37.875: E/AndroidRuntime(14712): at android.os.Handler.handleCallback(Handler.java:587) 04-01 22:33:37.875: E/AndroidRuntime(14712): at android.os.Handler.dispatchMessage(Handler.java:92) 04-01 22:33:37.875: E/AndroidRuntime(14712): at android.os.Looper.loop(Looper.java:130) 04-01 22:33:37.875: E/AndroidRuntime(14712): at android.app.ActivityThread.main(ActivityThread.java:3703) 04-01 22:33:37.875: E/AndroidRuntime(14712): at java.lang.reflect.Method.invokeNative(Native Method) 04-01 22:33:37.875: E/AndroidRuntime(14712): at java.lang.reflect.Method.invoke(Method.java:507) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 04-01 22:33:37.875: E/AndroidRuntime(14712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 04-01 22:33:37.875: E/AndroidRuntime(14712): at dalvik.system.NativeStart.main(Native Method)

I know the problem is my usage of the method matchinfos(). How to solve it?

rayboot avatar Apr 01 '13 15:04 rayboot

Did you solve the issue? If so, how?

mhamann avatar Jun 15 '13 19:06 mhamann