所以我得到了这个JSON:
{ "games": [
{
"location": "germany",
"team1": {
"name": "test1",
"tore": "2"
},
"team2": {
"name": "test2",
"tore": "3"
}
},...
我的这个JSON的模型类看起来像那样(我认为我得到了错误)
MODEL CLASS:
import com.google.gson.annotations.SerializedName;
public class GameModel {
public String location;
@SerializedName("team1")
public Team team1;
@SerializedName("team2")
public Team team2;
public String getLocation() {
return zeitpunkt;
}
public void setLocation(String location) {
this.location= location;
}
class Team
public String name;
public String tore;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTore() {
return tore;
}
public void setTore(String tore) {
this.tore = tore;
}
@Override
public String toString() {
return "Team{" +
name='" + name + '\'' +
", tore='" + tore + '\'' +
'}';
}
}
我的GSON看起来就像那样..
GSON课程
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("games");
gameModelList = new ArrayList<>();
Gson gson2 = new Gson();
for(int i=0; i<parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
GameModel inputGame = gson2.fromJson(finalObject.toString(), GameModel.class);
gameModelList.add(inputGame);
}
return gameModelList.toString();
games
的位置可以在我的CustomAdapter类中调用,但每次我都希望获得team1的名称
GameModel gm = getItem(position);
tvTeamOne.setText("" +gm.team1.getName());
我得到java.null.lang pointer exception
我也尝试了调试器,它显示了team1:null
,所以他没有得到String
logcat的:
07-04 13:26:53.720 25600-25600/de.xxxxxxx.app.mc.xxxxxx E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at de.xxxxx.app.mc.xxxxx.GameAdapter.getView(GameAdapter.java:40)
at android.widget.AbsListView.obtainView(AbsListView.java)
at android.widget.ListView.measureHeightOfChildren(ListView.java)
at android.widget.ListView.onMeasure(ListView.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at android.view.View.measure(View.java)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java)
at android.widget.LinearLayout.measureVertical(LinearLayout.java)
at android.widget.LinearLayout.onMeasure(LinearLayout.java)
at android.view.View.measure(View.java)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java)
at android.widget.FrameLayout.onMeasure(FrameLayout.java)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java)
at android.view.View.measure(View.java)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java)
at android.view.Choreographer.doCallbacks(Choreographer.java)
at android.view.Choreographer.doFrame(Choreographer.java)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
首先创建模型:
public class Team1
{
public String name;
public String tore;
}
public class Team2
{
public String name;
public String tore;
}
public class Game
{
public String location;
public Team1 team1;
public Team2 team2;
}
public class YourRootObject
{
public List<Game> games;
}
然后简单解析到对象:
Gson gson = new Gson();
try {
YourRootObject result = (YourRootObject) gson.fromJson(yourJson, YourRootObject.class);
catch(Exception e){}
并且在结果中所有数据(如果存在)将被正确解析并且您将在值
中获得空值答案 1 :(得分:0)
这里的所有代码都是无关紧要的。只有“GameAdapter”getview是必需的。我认为既然你在调用“getItem(position)”
您需要像这样更改此方法。
class A {
int i = 10;
public void printValue() {
System.out.println("Value-A");
}
}
class B extends A{
int i = 12;
public void printValue() {
System.out.print("Value-B" + i);
}
}
public class Test{
public static void main(String argv[]){
A a = new B();
a.printValue();
System.out.println("===>"+a.i); // This line print 10 instead of 12
}
}
希望这有帮助。