问题描述:

I have a class that works until I try to use JSONFunkcije class. (remove comments)

TaksiDetalji.java:

public class TaksiDetalji extends Activity {

String id;

String ime;

JSONFunkcije podatak;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.taksi_detalji);

Intent i = getIntent();

// getting id

id = i.getStringExtra("id");

podatak = = new JSONFunkcije(id);

List<String> sluzbice = new ArrayList<String>();

sluzbice = podatak.DetaljiSluzbe();

//EditText editText = (EditText)findViewById(R.id.editText1);

//Iterator <String> it = sluzbice.iterator();

//ime = it.next();

//editText.setText("ime");

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.taksi_detalji, menu);

return true;

}

}

When I remove comments I get the following error: (Unfortunately, Oceni Taksi has stopped = no layout loaded)

09-26 21:29:26.258: E/AndroidRuntime(5815): FATAL EXCEPTION: main

09-26 21:29:26.258: E/AndroidRuntime(5815): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ocenitaksi/com.example.ocenitaksi.TaksiDetalji}: java.util.NoSuchElementException

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.ActivityThread.access$600(ActivityThread.java:141)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.os.Handler.dispatchMessage(Handler.java:99)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.os.Looper.loop(Looper.java:137)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.ActivityThread.main(ActivityThread.java:5041)

09-26 21:29:26.258: E/AndroidRuntime(5815): at java.lang.reflect.Method.invokeNative(Native Method)

09-26 21:29:26.258: E/AndroidRuntime(5815): at java.lang.reflect.Method.invoke(Method.java:511)

09-26 21:29:26.258: E/AndroidRuntime(5815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

09-26 21:29:26.258: E/AndroidRuntime(5815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

09-26 21:29:26.258: E/AndroidRuntime(5815): at dalvik.system.NativeStart.main(Native Method)

09-26 21:29:26.258: E/AndroidRuntime(5815): Caused by: java.util.NoSuchElementException

09-26 21:29:26.258: E/AndroidRuntime(5815): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:572)

09-26 21:29:26.258: E/AndroidRuntime(5815): at com.example.ocenitaksi.TaksiDetalji.onCreate(TaksiDetalji.java:32)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.Activity.performCreate(Activity.java:5104)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

09-26 21:29:26.258: E/AndroidRuntime(5815): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

09-26 21:29:26.258: E/AndroidRuntime(5815): ... 11 more

JSONfunkcije.java

package com.example.ocenitaksi;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.NameValuePair;

import org.apache.http.message.BasicNameValuePair;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

public class JSONFunkcije {

List<String> niz;

private String ime_sluzbe;

private String id_taksiste;

//konstruktori

public JSONFunkcije(String ime_sluzbe , String id_taksiste){

this.ime_sluzbe=ime_sluzbe;

this.id_taksiste=id_taksiste;

}

public JSONFunkcije(String ime_sluzbe){

this.ime_sluzbe=ime_sluzbe;

}

//parser za zahteve

JSONParser jParser = new JSONParser();

JSONArray products = null;

//adrese

private static String sluzbe = "http://android.yunews.net/sluzbe.php";

private static String detalji_sluzba = "http://android.yunews.net/detalji_sluzba.php";

private static String detalji_taksiste = "http://android.yunews.net/detalji_taksiste.php";

//JSON TAGOVI

private static final String TAG_NAME = "ime";

private static final String TAG_SUCCESS = "success";

private static final String TAG_PRODUCTS = "products";

private static final String TAG_BROJ = "broj";

private static final String TAG_GLASOVI ="glasovi";

private static final String TAG_TEKST = "tekst";

private static final String TAG_OCENA = "ocena";

private static final String TAG_KOMENTARI="komentari";

protected List<String> ListaSluzbi (){

niz=new ArrayList<String>();

List<NameValuePair> params = new ArrayList<NameValuePair>();

JSONObject json = jParser.makeHttpRequest(sluzbe, "GET", params);

try {

// Checking for SUCCESS TAG

int success = json.getInt(TAG_SUCCESS);

if (success == 1) {

// products found

// Getting Array of Products

products = json.getJSONArray(TAG_PRODUCTS);

// looping through All Products

int i=0;

for ( i = 0; i < products.length(); i++) {

JSONObject c = products.getJSONObject(i);

niz.add(c.getString(TAG_NAME));

}

}

}

catch (JSONException e) {

e.printStackTrace();

}

return niz;

}

protected List<String> DetaljiSluzbe (){

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("ime",ime_sluzbe));

JSONObject json = jParser.makeHttpRequest(detalji_sluzba,"POST", params);

params=new ArrayList<NameValuePair>();

//json=jParser.makeHttpRequest(detalji_sluzba,"GET", params);

try {

// Checking for SUCCESS TAG

int success = json.getInt(TAG_SUCCESS);

if (success == 1) {

// products found

// Getting Array of Products

products = json.getJSONArray(TAG_PRODUCTS);

// looping through All Products

int i=0;

for ( i = 0; i < products.length(); i++) {

JSONObject c = products.getJSONObject(i);

niz.add(c.getString(TAG_NAME));

niz.add(c.getString(TAG_BROJ));

niz.add(c.getString(TAG_GLASOVI));

niz.add(c.getString(TAG_OCENA));

niz.add(c.getString(TAG_TEKST));

}

}

}

catch (JSONException e) {

e.printStackTrace();

}

return niz;

}

protected List<String> DetaljiTaksiste (){

niz=new ArrayList<String>();

List<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("sluzba",ime_sluzbe));

params.add(new BasicNameValuePair("taksista",id_taksiste));

JSONObject json = jParser.makeHttpRequest(detalji_sluzba,"POST", params);

params=new ArrayList<NameValuePair>();

//json=jParser.makeHttpRequest(detalji_taksiste,"GET", params);

try {

// Checking for SUCCESS TAG

int success = json.getInt(TAG_SUCCESS);

if (success == 1) {

// products found

// Getting Array of Products

products = json.getJSONArray(TAG_PRODUCTS);

// looping through All Products

int i=0;

for ( i = 0; i < products.length(); i++) {

JSONObject c = products.getJSONObject(i);

niz.add(c.getString(TAG_OCENA));

niz.add(c.getString(TAG_GLASOVI));

niz.add(c.getString(TAG_KOMENTARI));

}

}

}

catch (JSONException e) {

e.printStackTrace();

}

return niz;

}

}

I think that the problem in JSON and I am sure that the problem is in the JSONfunkcije.java but I can't find anything.

Tnx,

Djomla

网友答案:

The problem is in JSONFunkcije.java in DetaljiSluzba().

I forgot to put niz=new ArrayList<String>();

Now works. Tnx.

相关阅读:
Top