Dima Kiltau Dima Kiltau - 2 months ago 11
Android Question

JSON parsing with left and right swipe navigation

I have a problem with my android app. It won't display my json items how I want them.

If I open the app it should display the first mission and if I swipe left it should display the next mission (swipe right should display the previous mission).

I know that there is some unneeded and false code..

json data looks like this:

{
"missionen": [
{
"nr": 1,
"n": "n1",
"sd": "sd1",
"oc": "oc1",
"s": "s1"
},
{
"nr": 2,
"n": "n2",
"sd": "sd2",
"oc": "oc2",
"s": "s2"
}
]
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.xairon.colm.MainActivity">

<!-- view for AdMob Banner Ad -->

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mission:"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/tvNr"
android:textSize="21sp"
android:layout_margin="10dp"
android:textColor="@color/colorPrimaryDark" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
android:id="@+id/tvN"
android:textColor="@color/colorPrimaryDark"
android:textSize="23sp"
android:layout_above="@+id/tvSd"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start date"
android:id="@+id/tvSd"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:layout_above="@+id/tvOc"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="organisation, country"
android:id="@+id/tvOc"
android:textColor="@color/colorPrimaryDark"
android:textSize="18sp"
android:layout_above="@+id/tvS"
android:layout_centerHorizontal="true"
android:layout_margin="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stage"
android:id="@+id/tvS"
android:textColor="@color/colorPrimaryDark"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>


and my MainActivity.java

package net.xairon.colm;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

public TextView tv_nr, tv_n, tv_sd, tv_oc, tv_s;
public int count = 0;
public String m;
InputStream inputstream;
BufferedReader reader;
ArrayList<MissionClass> missions;

public class MissionClass {
public int nr;
public String n, sd, oc, s;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_main, null);
setContentView(view);

AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);

try {
inputstream = getResources().getAssets().open("colm_new.json");
reader = new BufferedReader(new InputStreamReader(inputstream));

m = reader.toString();
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
}
m = total.toString();
} catch (IOException ex) {
ex.printStackTrace();
}

count++;
setValues(count);

view.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
public void onSwipeTop() {
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
if (count == missions.size() - 1) {
finish();
} else {
count++;
setValues(count);
}
}
public void onSwipeBottom() {
}
});
}

public void JSONParser() {
missions = new ArrayList<>();

try {
JSONObject jsonObjMain = new JSONObject(m);
JSONArray jsonArray = jsonObjMain.getJSONArray("missionen");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jO = jsonArray.getJSONObject(i);

MissionClass missionClass = new MissionClass();
missionClass.nr = jO.getInt("nr");
missionClass.n = jO.getString("n");
missionClass.sd = jO.getString("sd");
missionClass.oc =jO.getString("oc");
missionClass.s = jO.getString("s");
missions.add(missionClass);
}

} catch (JSONException e) {
e.printStackTrace();
}
}

public void setValues(int j) {
tv_nr.setText(missions.get(j).nr);
tv_n.setText(missions.get(j).n);
tv_sd.setText(missions.get(j).sd);
tv_oc.setText(missions.get(j).oc);
tv_s.setText(missions.get(j).s);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

if (id == R.id.action_about) {
return true;
}

return super.onOptionsItemSelected(item);
}

}


Error log

09-10 20:16:58.131 16906-16906/net.xairon.colm E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.xairon.colm, PID: 16906
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.xairon.colm/net.xairon.colm.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
at net.xairon.colm.MainActivity.setValues(MainActivity.java:111)
at net.xairon.colm.MainActivity.onCreate(MainActivity.java:66)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:158) 
at android.app.ActivityThread.main(ActivityThread.java:7224) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Answer

Create Class as follows

Class MyClass{
    public int nr;
    public String n;
    public String sd;
    public String oc;
    public String s;
}

and create List like follows

ArrayList<MyClass> missions = new ArrayList<>();

and parse your json as follows

JSONObject jsonObjMain = new JSONObject(m);
JSONArray jsonArray = jsonObjMain.getJSONArray("missionen");

for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject jO = jsonArray.getJSONObject(i);

    MyClass myClass = new MyClass();
    myClass.nr = jO.getInt("nr");
    myClass.n = jO.getString("n");
    myClass..sd = jO.getString("sd");
    myClass.oc = jO.getString("oc");
    myClass.s = jO.getString("s");
    missions.add(myClass);
}

Edit - Also your setValues() method should be as follows

public void setValues(int j) {
    tv_nr.setText(""+missions.get(j).nr);
    tv_n.setText(missions.get(j).n);
    tv_sd.setText(missions.get(j).sd);
    tv_oc.setText(missions.get(j).oc);
    tv_s.setText(missions.get(j).c);
}
Comments