Android类型不匹配:无法从JSONArray转换为ArrayList <hashmap <string,string>&gt; </hashmap <string,string>

时间:2015-01-08 09:41:30

标签: java android string arraylist

我试图在片段中制作列表视图,但我得到了这个。我在评论中加入了错误注释。我不知道为什么因为我是android的新手,所以有人请帮帮我

此行有多个标记

  • 类型不匹配:无法从JSONArray转换为ArrayList&gt;
  • 对于类型ArrayList&gt;
  • ,方法length()未定义
  • 方法getJSONObject(int)未定义类型ArrayList&gt;

FragmentaReviews.java

public class FragmentaReviews extends SherlockFragment {

private JSONReview json;
ListView list;
TextView title;
ArrayList<HashMap<String, String>> ListReview = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "http:/10.0.3.2/1aaa/listreview.php";

//JSON Node Names
private static final String TAG_Review = "ListReview";
private static final String TAG_title = "title";

JSONArray reviews = null;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.row_main, container, false);


}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

   ListReview = new ArrayList<HashMap<String, String>>();

            new JSONParse().execute();  

}


private class JSONParse extends AsyncTask<String, String, JSONObject> {

    private ProgressDialog pDialog;
    JSONReview jParser = new JSONReview();
   // json = new JSONReview();
    private JSONObject json;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        title = (TextView)getView().findViewById(R.id.title);

        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();


    }

    @Override
    protected JSONObject doInBackground(String... args) {

        // Getting JSON from URL
        json = jParser.getJSONFromURL(url);

        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {

        pDialog.dismiss();
        try {


             //Change here
             // Getting JSON Array from URL 
            JSONArray jsonArray = json.getJSONArray(TAG_Review);

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

                 JSONObject c = jsonArray.getJSONObject(i);

               //Stop here



                 // Storing  JSON item in a Variable
                 String title = c.getString(TAG_title);

                 // Adding value HashMap key => value
                 HashMap<String, String> map = new HashMap<String, String>();
                 map.put(TAG_title, title);



                 ListReview.add(map);

                 list = (ListView)getView().findViewById(R.id.list);

                 ListAdapter adapter = new SimpleAdapter(getActivity(), ListReview,
                     R.layout.fragmenta_review,
                     new String[] { TAG_title, TAG_tujuanwisata}, new int[] {
                         R.id.title);
                 list.setAdapter(adapter);
                 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                     @Override
                     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

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

JSONReview.java

public class JSONReview
{
private InputStream     is              = null;
private JSONObject      jsonObject      = null;
private String          json            = "";


private  final String TAG_Review = "ListReview";
private final String TAG_IDR ="id";
private  final String TAG_title = "title";


public JSONObject getJSONFromURL(String url)
{
    try
    {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    } catch (ClientProtocolException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }

    try
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);

        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }

        is.close();
        json = sb.toString();
    } catch (Exception e)
    {
        // TODO: handle exception
    }

    try
    {
        jsonObject = new JSONObject(json);

    } catch (JSONException e)
    {
        // TODO: handle exception
    }

    return jsonObject;
}       



//--------------------- review
public ArrayList<Review> getJSONArray(JSONObject jobj)
{
    ArrayList<Review> ListReview = new ArrayList<Review>();

    try
    {
        JSONArray arrayListReview = jobj.getJSONArray(TAG_Review);

        for (int i = 0; i < arrayListReview.length(); i++)
        {
            JSONObject jobject = arrayListReview.getJSONObject(i);
            ListReview.add(new Review(jobject.getInt(TAG_IDR), jobject.getString(TAG_title)));

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

review.java

public class Review  {

private int id;
private String tujuanwisata;
private String judul;
private String isi;


public Review()
{
    // do nothing
}

public Review (int id, String tujuanwisata, String judul, String isi) {
    super();
    this.id =id;
    this.tujuanwisata = tujuanwisata;
    this.judul = judul;
    this.isi = isi;

}

public int getid() {
    return id;
}

public void setid(int id) {
    this.id = id;
}


public String gettujuanwisata() {
    return tujuanwisata;
}

public void settujuanwisata(String tujuanwisata) {
    this.tujuanwisata = tujuanwisata;
}

public String getjudul() {
    return judul;
}

public void setjudul(String judul) {
    this.judul = judul;
}

public String getisi() {
    return isi;
}

public void setisi(String isi) {
    this.isi = isi;
}
}

logcat的

  01-08 17:47:32.555: E/WindowManager(2186): Activity com.android.GoTrip.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{527c304c V.E..... R......D 0,0-684,192} that was originally added here
  01-08 17:47:32.555: E/WindowManager(2186): android.view.WindowLeaked: Activity com.android.GoTrip.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{527c304c V.E..... R......D 0,0-684,192} that was originally added here
  01-08 17:47:32.555: E/WindowManager(2186):    at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
  01-08 17:47:32.555: E/WindowManager(2186):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.app.Dialog.show(Dialog.java:281)
 01-08 17:47:32.555: E/WindowManager(2186):     at com.android.GoTrip.FragmentaReviews$JSONParse.onPreExecute(FragmentaReviews.java:92)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.os.AsyncTask.execute(AsyncTask.java:534)
 01-08 17:47:32.555: E/WindowManager(2186):     at com.android.GoTrip.FragmentaReviews.onActivityCreated(FragmentaReviews.java:70)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
 01-08 17:47:32.555: E/WindowManager(2186):     at  android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:851)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 01-08 17:47:32.555: E/WindowManager(2186):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
 01-08 17:47:32.555: E/WindowManager(2186):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.View.measure(View.java:15848)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.Choreographer.doFrame(Choreographer.java:532)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.os.Handler.handleCallback(Handler.java:730)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.os.Handler.dispatchMessage(Handler.java:92)
 01-08 17:47:32.555: E/WindowManager(2186):     at android.os.Looper.loop(Looper.java:137)
 01-08 17:47:32.555: E/WindowManager(2186):     at  android.app.ActivityThread.main(ActivityThread.java:5103)
 01-08 17:47:32.555: E/WindowManager(2186):     at java.lang.reflect.Method.invokeNative(Nati

2 个答案:

答案 0 :(得分:0)

这里:

  

ListReview = json.getJSONArray(TAG_Review);

您正尝试将从JSONArray返回的json.getJSONArray(TAG_Review)分配给ListReview List对象。从JSONObject获取JSONArray:

JSONArray jsonArray = json.getJSONArray(TAG_Review);

并使用jsonArray代替ListReview来获取JSONArray的长度和值。

答案 1 :(得分:0)

你宣布了一个成员变量

ArrayList<HashMap<String, String>> ListReview = new ArrayList<HashMap<String, String>>();

是HashMap的ArrayList,在你的AsyncTask中,你试图为这个对象分配getJSONArray的返回值,这又是JSONArray,导致编译时错误< / p>

更改

ListReview = json.getJSONArray(TAG_Review);

JSONArrary array = json.getJSONArray(TAG_Review);

并使用array进行解析