尝试从Fragment中解析Listview中的JSON文件

时间:2014-10-23 02:58:42

标签: android mysql json fragment

刚刚在Android和其他地方找到了许多使用PHP进行JSON解析的示例,并尝试使用我的项目。在活动中它没关系,但改成片段它不起作用。我认为这是一个愚蠢的错误,我得知它。

片段代码:

package com.example.waitersoriginal;

import java.util.ArrayList;
import java.util.HashMap;

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

import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListaFragment extends ListFragment {

private ProgressDialog pDialog;

 private static final String URL_SELECT ="http://localhost/waiters/select_bebidas.php";   

//JSONTAGS
private static final String TAG_NOME = "NOME";
private static final String TAG_BEBIDAS = "bebidas";
private static final String TAG_DESCR = "DESCR";

private JSONArray mProdutos = null;
private ArrayList<HashMap<String, String>> mProdutosList;


public ListaFragment(){}

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{ 
  return inflater.inflate(R.layout.lista_fragment, container, false); 
}

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //loading the comments via AsyncTask
    new LoadEvents().execute();

  }
  public void updateJSONdata() {

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


    JSONParser jParser = new JSONParser();

    JSONObject json = jParser.getJSONFromUrl(URL_SELECT);

    try {


        mProdutos = json.getJSONArray(TAG_BEBIDAS);


        for (int i = 0; i < mProdutos.length(); i++) {
            JSONObject c = mProdutos.getJSONObject(i);

            String nome = c.getString(TAG_NOME);
            String descr = c.getString(TAG_DESCR);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            map.put(TAG_NOME, nome);
            map.put(TAG_DESCR, descr);

            mProdutosList.add(map);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
private void updateList() {


ListAdapter adapter = new SimpleAdapter(getActivity(), mProdutosList,
        R.layout.list_item, 
        new String[] { TAG_NOME, TAG_DESCR}, 
        new int[] { R.id.pid, R.id.name});

            setListAdapter(adapter);

            ListView lv = getListView();    
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    }            
                });
}

public class LoadEvents extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading Events...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... arg0) {

        updateJSONdata();
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();

        updateList();
        }
      }
   }

Logcat错误:

    10-22 22:41:40.814: E/AndroidRuntime(2513): FATAL EXCEPTION: main
    10-22 22:41:40.814: E/AndroidRuntime(2513): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitersoriginal/com.example.waitersoriginal.BebidaActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.os.Handler.dispatchMessage(Handler.java:99)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.os.Looper.loop(Looper.java:137)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at java.lang.reflect.Method.invokeNative(Native Method)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at java.lang.reflect.Method.invoke(Method.java:525)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at dalvik.system.NativeStart.main(Native Method)
    10-22 22:41:40.814: E/AndroidRuntime(2513): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class fragment
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.Activity.setContentView(Activity.java:1895)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at com.example.waitersoriginal.BebidaActivity.onCreate(BebidaActivity.java:19)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.Activity.performCreate(Activity.java:5133)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     ... 11 more
    10-22 22:41:40.814: E/AndroidRuntime(2513): Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ListFragment.ensureList(ListFragment.java:402)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.ListFragment.onViewCreated(ListFragment.java:203)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.app.Activity.onCreateView(Activity.java:4746)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
    10-22 22:41:40.814: E/AndroidRuntime(2513):     ... 21 more

有什么想法吗?

谢谢大家。

编辑:

lista_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

编辑2:

Logcat错误:

10-22 23:46:11.842: W/System.err(2729): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
10-22 23:46:12.062: W/System.err(2729):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
10-22 23:46:12.192: W/System.err(2729):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-22 23:46:12.212: W/System.err(2729):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-22 23:46:12.262: W/System.err(2729):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-22 23:46:12.292: W/System.err(2729):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-22 23:46:12.342: W/System.err(2729):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-22 23:46:12.352: W/System.err(2729):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-22 23:46:12.362: W/System.err(2729):     at com.example.waitersoriginal.JSONParser.getJSONFromUrl(JSONParser.java:30)
10-22 23:46:12.362: W/System.err(2729):     at com.example.waitersoriginal.ListaFragment.updateJSONdata(ListaFragment.java:60)
10-22 23:46:12.392: W/System.err(2729):     at com.example.waitersoriginal.ListaFragment$LoadEvents.doInBackground(ListaFragment.java:122)
10-22 23:46:12.392: W/System.err(2729):     at com.example.waitersoriginal.ListaFragment$LoadEvents.doInBackground(ListaFragment.java:1)
10-22 23:46:12.412: W/System.err(2729):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-22 23:46:12.422: W/System.err(2729):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-22 23:46:12.442: W/System.err(2729):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-22 23:46:12.462: W/System.err(2729):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-22 23:46:12.462: W/System.err(2729):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-22 23:46:12.472: W/System.err(2729):     at java.lang.Thread.run(Thread.java:841)
10-22 23:46:12.492: W/System.err(2729): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
10-22 23:46:12.512: W/System.err(2729):     at libcore.io.IoBridge.connect(IoBridge.java:114)
10-22 23:46:12.512: W/System.err(2729):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-22 23:46:12.512: W/System.err(2729):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-22 23:46:12.552: W/System.err(2729):     at java.net.Socket.connect(Socket.java:842)
10-22 23:46:12.562: W/System.err(2729):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-22 23:46:12.562: W/System.err(2729):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-22 23:46:12.562: W/System.err(2729):     ... 16 more
10-22 23:46:12.562: W/System.err(2729): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
10-22 23:46:12.592: W/System.err(2729):     at libcore.io.Posix.connect(Native Method)
10-22 23:46:12.652: W/System.err(2729):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-22 23:46:12.652: W/System.err(2729):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-22 23:46:12.662: W/System.err(2729):     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-22 23:46:12.662: W/System.err(2729):     ... 21 more
10-22 23:46:12.662: E/Buffer Error(2729): Error converting result java.lang.NullPointerException: lock == null
10-22 23:46:12.702: E/JSON Parser(2729): Error parsing data org.json.JSONException: End of input at character 0 of 
10-22 23:46:12.702: W/dalvikvm(2729): threadid=11: thread exiting with uncaught exception (group=0xb2f30648)
10-22 23:46:12.712: E/AndroidRuntime(2729): FATAL EXCEPTION: AsyncTask #1
10-22 23:46:12.712: E/AndroidRuntime(2729): java.lang.RuntimeException: An error occured while executing doInBackground()
10-22 23:46:12.712: E/AndroidRuntime(2729):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at java.lang.Thread.run(Thread.java:841)
10-22 23:46:12.712: E/AndroidRuntime(2729): Caused by: java.lang.NullPointerException
10-22 23:46:12.712: E/AndroidRuntime(2729):     at com.example.waitersoriginal.ListaFragment.updateJSONdata(ListaFragment.java:65)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at com.example.waitersoriginal.ListaFragment$LoadEvents.doInBackground(ListaFragment.java:122)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at com.example.waitersoriginal.ListaFragment$LoadEvents.doInBackground(ListaFragment.java:1)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-22 23:46:12.712: E/AndroidRuntime(2729):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-22 23:46:12.712: E/AndroidRuntime(2729):     ... 4 more
10-22 23:46:13.412: W/EGL_emulation(2729): eglSurfaceAttrib not implemented
10-22 23:46:14.142: E/WindowManager(2729): Activity com.example.waitersoriginal.BebidaActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b320ffd0 V.E..... R......D 0,0-576,96} that was originally added here
10-22 23:46:14.142: E/WindowManager(2729): android.view.WindowLeaked: Activity com.example.waitersoriginal.BebidaActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b320ffd0 V.E..... R......D 0,0-576,96} that was originally added here
10-22 23:46:14.142: E/WindowManager(2729):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
10-22 23:46:14.142: E/WindowManager(2729):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
10-22 23:46:14.142: E/WindowManager(2729):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.Dialog.show(Dialog.java:281)
10-22 23:46:14.142: E/WindowManager(2729):  at com.example.waitersoriginal.ListaFragment$LoadEvents.onPreExecute(ListaFragment.java:117)
10-22 23:46:14.142: E/WindowManager(2729):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-22 23:46:14.142: E/WindowManager(2729):  at android.os.AsyncTask.execute(AsyncTask.java:534)
10-22 23:46:14.142: E/WindowManager(2729):  at com.example.waitersoriginal.ListaFragment.onResume(ListaFragment.java:50)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.Fragment.performResume(Fragment.java:1738)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1039)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1850)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.Activity.performResume(Activity.java:5221)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2780)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2266)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-22 23:46:14.142: E/WindowManager(2729):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 23:46:14.142: E/WindowManager(2729):  at android.os.Looper.loop(Looper.java:137)
10-22 23:46:14.142: E/WindowManager(2729):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-22 23:46:14.142: E/WindowManager(2729):  at java.lang.reflect.Method.invokeNative(Native Method)
10-22 23:46:14.142: E/WindowManager(2729):  at java.lang.reflect.Method.invoke(Method.java:525)
10-22 23:46:14.142: E/WindowManager(2729):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-22 23:46:14.142: E/WindowManager(2729):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-22 23:46:14.142: E/WindowManager(2729):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您已经将您的类提到了ListFragment,这意味着您的xml应该只有一个listview,其中包含一个LinearLayout和一个ListView。你的xml应该是这样的......

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /> 

如果您不想更改xml,请将您的班级更改为片段

public class ListaFragment extends Fragment 

手动查找列表视图

    ListView listView;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState){
            ctx=getActivity();
            LayoutInflater m_inflater = LayoutInflater.from(getActivity());
            rootView = m_inflater.inflate(R.layout.lista_fragment, null);
listView=rootView.findViewById(R.id.listView1);
            return rootView;
    }

从那里拿走。