Android ListFragment不填充

Android ListFragment不填充,android,json,Android,Json,因此,我正在构建一个应用程序,从这个页面获取数据:并将其放入这个片段中的listview中。我正在使用ListFragment。我最难编译所有内容,尤其是在oncreateview()中返回视图时。我得到了这个错误: 我通常可以阅读logcat并了解我需要查看的位置,但我查看了第299行,它是一个有注释的部分。。。应用程序开始加载进程控制盘,但很快就会崩溃,然后从URI填充一系列联系人。我不明白logcat想告诉我什么。有人能告诉我该往哪个方向走吗 以下是我的主要活动: public clas

因此,我正在构建一个应用程序,从这个页面获取数据:并将其放入这个片段中的listview中。我正在使用ListFragment。我最难编译所有内容,尤其是在oncreateview()中返回视图时。我得到了这个错误:

我通常可以阅读logcat并了解我需要查看的位置,但我查看了第299行,它是一个有注释的部分。。。应用程序开始加载进程控制盘,但很快就会崩溃,然后从URI填充一系列联系人。我不明白logcat想告诉我什么。有人能告诉我该往哪个方向走吗

以下是我的主要活动:

public class MainActivity extends Activity {

// Within which the entire activity is enclosed
DrawerLayout mDrawerLayout;

// ListView represents Navigation Drawer
ListView mDrawerList;

// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
ActionBarDrawerToggle mDrawerToggle;

// Title of the action bar
String mTitle="";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = (String) getTitle();

    // Getting reference to the DrawerLayout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerList = (ListView) findViewById(R.id.drawer_list);

    // Getting reference to the ActionBarDrawerToggle
    mDrawerToggle = new ActionBarDrawerToggle( this,
        mDrawerLayout,
        R.drawable.ic_drawer,
        R.string.drawer_open,
        R.string.drawer_close){

            /** Called when drawer is closed */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu();
            }

            /** Called when a drawer is opened */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Select Option");
                invalidateOptionsMenu();
            }
    };

    // Setting DrawerToggle on DrawerLayout
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // Creating an ArrayAdapter to add items to the listview mDrawerList
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getBaseContext(),
        R.layout.drawer_list_item ,
        getResources().getStringArray(R.array.options)
    );

    // Setting the adapter on mDrawerList
    mDrawerList.setAdapter(adapter);

    // Enabling Home button
    getActionBar().setHomeButtonEnabled(true);

    // Enabling Up navigation
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // Setting item click listener for the listview mDrawerList
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // Getting an array of options
                String[] options = getResources().getStringArray(R.array.options);

                //Currently selected option
                mTitle = options[position];

                if(mTitle.equalsIgnoreCase("Home")){
                    // Creating a fragment object
                    Fragment fragment = new AppMain();

                    // Creating a Bundle object
                    Bundle data = new Bundle();

                    // Setting the index of the currently selected item of mDrawerList
                    data.putInt("position", position);

                    // Setting the position to the fragment
                    fragment.setArguments(data);

                    // Getting reference to the FragmentManager
                    FragmentManager fragmentManager = getFragmentManager();

                    // Creating a fragment transaction
                    FragmentTransaction ft = fragmentManager.beginTransaction();

                    // Adding a fragment to the fragment transaction
                    ft.replace(R.id.content_frame, fragment);

                    // Committing the transaction
                    ft.commit();

                    // Closing the drawer
                    mDrawerLayout.closeDrawer(mDrawerList);

                }

                else if(mTitle.equalsIgnoreCase("Map")){
                    // Creating a fragment object
                    Fragment fragment = new MapFragment();

                    // Creating a Bundle object
                    Bundle data = new Bundle();

                    // Setting the index of the currently selected item of mDrawerList
                    data.putInt("position", position);

                    // Setting the position to the fragment
                    fragment.setArguments(data);

                    // Getting reference to the FragmentManager
                    FragmentManager fragmentManager = getFragmentManager();

                    // Creating a fragment transaction
                    FragmentTransaction ft = fragmentManager.beginTransaction();

                    // Adding a fragment to the fragment transaction
                    ft.replace(R.id.content_frame, fragment);

                    // Committing the transaction
                    ft.commit();

                    // Closing the drawer
                    mDrawerLayout.closeDrawer(mDrawerList);

                }
                //its possible to create a series of these and then 
                //initialize objects within the fragments programmably. 

                //DrawerOptions need to be removed to make things work
                else if(mTitle.equalsIgnoreCase("ATO")){

               // Creating a fragment object
                    Fragment fragment = new ATO();

                    // Creating a Bundle object
                    Bundle data = new Bundle();

                    // Setting the index of the currently selected item of mDrawerList
                    data.putInt("position", position);

                    // Setting the position to the fragment
                    fragment.setArguments(data);

                    // Getting reference to the FragmentManager
                    FragmentManager fragmentManager = getFragmentManager();

                    // Creating a fragment transaction
                    FragmentTransaction ft = fragmentManager.beginTransaction();

                    // Adding a fragment to the fragment transaction
                    ft.replace(R.id.content_frame, fragment);

                    // Committing the transaction
                    ft.commit();

                // Closing the drawer
                mDrawerLayout.closeDrawer(mDrawerList);

                // Closing the drawer
                mDrawerLayout.closeDrawer(mDrawerList);
        }
                else{

                }
       }
    });
}

    @Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

/** Handling the touch event of app icon */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/*
 * I commented out this section below because I don't see us using it.
 * It does nothing more than show a little button which says "settings"
 * in the top right corner of the bar. If you want to enable it, de-comment
 * and then look at the main.xml within the menu folder and de-commnent there.
 * Also have to go to the Strings.xml file under values and de-comment.
 */

/** Called whenever we call invalidateOptionsMenu() */
   @Override
//    public boolean onPrepareOptionsMenu(Menu menu) {
//        // If the drawer is open, hide action items related to the content view
//        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
// 
//        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
//        return super.onPrepareOptionsMenu(menu);
//    }
// 

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    } 
}
公共类MainActivity扩展活动{
//其中包含整个活动
抽屉布局mDrawerLayout;
//ListView表示导航抽屉
ListView mDrawerList;
//ActionBarDrawerToggle表示操作栏中存在导航抽屉
ActionBarDrawerToggle mDrawerToggle;
//操作栏的标题
字符串mTitle=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle=(字符串)getTitle();
//获取对抽屉布局的引用
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
mDrawerList=(ListView)findViewById(R.id.drawer\u列表);
//获取对ActionBarDrawerToggle的引用
MDRAWERTOGLE=新操作BARDRAWERTOGLE(此,
mDrawerLayout,
R.可抽出式ic_抽屉,
R.string.drawer\u打开,
右弦抽屉(U关闭){
/**抽屉关闭时调用*/
公共无效onDrawerClosed(视图){
getActionBar().setTitle(mTitle);
无效操作菜单();
}
/**打开抽屉时调用*/
打开图纸上的公共空白(视图抽屉视图){
getActionBar().setTitle(“选择选项”);
无效操作菜单();
}
};
//设置抽屉在抽屉布局上切换
mDrawerLayout.setDrawerListener(mDrawerToggle);
//创建ArrayAdapter以将项添加到listview mDrawerList
ArrayAdapter适配器=新的ArrayAdapter(
getBaseContext(),
R.布局.抽屉清单项目,
getResources().getStringArray(R.array.options)
);
//在mDrawerList上设置适配器
mDrawerList.setAdapter(适配器);
//启用主页按钮
getActionBar().setHomeButtonEnabled(true);
//启用向上导航
getActionBar().setDisplayHomeAsUpEnabled(true);
//listview mDrawerList的设置项单击侦听器
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//获取一系列选项
String[]options=getResources().getStringArray(R.array.options);
//当前选择的选项
mTitle=选项[位置];
if(mTitle.equalsIgnoreCase(“主”)){
//创建片段对象
Fragment Fragment=newappmain();
//创建捆绑对象
Bundle data=新Bundle();
//设置mDrawerList当前选定项的索引
数据输入(“位置”,位置);
//将位置设置为片段
fragment.setArguments(数据);
//获取对FragmentManager的引用
FragmentManager FragmentManager=getFragmentManager();
//创建片段事务
FragmentTransaction ft=fragmentManager.beginTransaction();
//向片段事务添加片段
ft.replace(R.id.content\u frame,fragment);
//提交事务
ft.commit();
//关抽屉
mDrawerLayout.closeDrawer(mDrawerList);
}
else if(mTitle.equalsIgnoreCase(“Map”)){
//创建片段对象
Fragment Fragment=新的MapFragment();
//创建捆绑对象
Bundle data=新Bundle();
//设置mDrawerList当前选定项的索引
数据输入(“位置”,位置);
//将位置设置为片段
fragment.setArguments(数据);
//获取对FragmentManager的引用
FragmentManager FragmentManager=getFragmentManager();
//创建片段事务
FragmentTransaction ft=fragmentManager.beginTransaction();
//向片段事务添加片段
ft.replace(R.id.content\u frame,fragment);
//提交事务
ft.commit();
//关抽屉
mDrawerLayout.closeDrawer(mDrawerList);
}
//有可能创建一系列这些,然后
//以编程方式初始化片段中的对象。
//要使事情顺利进行,需要删除抽屉
否则,如果(mTitle.equalsIgnoreCase(“ATO”)){
//创建片段对象
片段=新的ATO();
//创建捆绑对象
Bundle data=新Bundle();
//设置mDrawerList当前选定项的索引
数据输入(“位置”,位置);
public class ATO extends ListFragment {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://api.androidhive.info/contacts/";

    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";

// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;

public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container, 
                         Bundle savedInstanceState) {

    // Retrieving the currently selected item number
    int position = getArguments().getInt("position");

    // List of options
    String[] options = getResources().getStringArray(R.array.options);

    // Creating view correspoding to the fragment
    View v = inflater.inflate(R.layout.ato, container, false);

    // Getting reference to the TextView of the Fragment
    TextView tv = (TextView) v.findViewById(R.id.textView1);

    String words = "Home of the app";
    // Setting currently selected river name in the TextView
    tv.setText(words);

    // Updating the action bar title
    getActivity().getActionBar().setTitle(options[position]);

    // Calling async task to get json
    new GetContacts().execute();

    return v;
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
         pDialog = new ProgressDialog(getActivity());
        //
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String email = c.getString(TAG_EMAIL);
                    String address = c.getString(TAG_ADDRESS);
                    String gender = c.getString(TAG_GENDER);

                    // Phone node is JSON Object
                    JSONObject phone = c.getJSONObject(TAG_PHONE);
                    String mobile = phone.getString(TAG_PHONE_MOBILE);
                    String home = phone.getString(TAG_PHONE_HOME);
                    String office = phone.getString(TAG_PHONE_OFFICE);

                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_ID, id);
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_EMAIL, email);
                    contact.put(TAG_PHONE_MOBILE, mobile);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), contactList,
                R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
                        TAG_PHONE_MOBILE }, new int[] { R.id.name,
                        R.id.email, R.id.mobile });

        setListAdapter(adapter);
    }

}

/*
 * {
"contacts": [
    {
            "id": "c200",
            "name": "Ravi Tamada",
            "email": "ravi@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c201",
            "name": "Johnny Depp",
            "email": "johnny_depp@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c202",
            "name": "Leonardo Dicaprio",
            "email": "leonardo_dicaprio@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c203",
            "name": "John Wayne",
            "email": "john_wayne@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c204",
            "name": "Angelina Jolie",
            "email": "angelina_jolie@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c205",
            "name": "Dido",
            "email": "dido@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c206",
            "name": "Adele",
            "email": "adele@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c207",
            "name": "Hugh Jackman",
            "email": "hugh_jackman@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c208",
            "name": "Will Smith",
            "email": "will_smith@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c209",
            "name": "Clint Eastwood",
            "email": "clint_eastwood@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c2010",
            "name": "Barack Obama",
            "email": "barack_obama@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c2011",
            "name": "Kate Winslet",
            "email": "kate_winslet@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "female",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    },
    {
            "id": "c2012",
            "name": "Eminem",
            "email": "eminem@gmail.com",
            "address": "xx-xx-xxxx,x - street, x - country",
            "gender" : "male",
            "phone": {
                "mobile": "+91 0000000000",
                "home": "00 000000",
                "office": "00 000000"
            }
    }
]
}
     */

}
package com.example.fratslidemenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.Inflater;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class ATO extends ListFragment {

private ProgressDialog pDialog;

// URL to get contacts JSON
private static String url = "http://api.androidhive.info/contacts/";

// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";

// contacts JSONArray
JSONArray contacts = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container, 
                         Bundle savedInstanceState) {

    // Retrieving the currently selected item number
    int position = getArguments().getInt("position");

    // List of options
    String[] options = getResources().getStringArray(R.array.options);

    // Creating view correspoding to the fragment
    View v = inflater.inflate(R.layout.ato, container, false);

    // Updating the action bar title
    getActivity().getActionBar().setTitle(options[position]);

    // Calling async task to get json
    new GetContacts().execute();

    return v;
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
         pDialog = new ProgressDialog(getActivity());
        //
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String email = c.getString(TAG_EMAIL);
                    String address = c.getString(TAG_ADDRESS);
                    String gender = c.getString(TAG_GENDER);

                    // Phone node is JSON Object
                    JSONObject phone = c.getJSONObject(TAG_PHONE);
                    String mobile = phone.getString(TAG_PHONE_MOBILE);
                    String home = phone.getString(TAG_PHONE_HOME);
                    String office = phone.getString(TAG_PHONE_OFFICE);

                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_ID, id);
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_EMAIL, email);
                    contact.put(TAG_PHONE_MOBILE, mobile);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), contactList,
                R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
                        TAG_PHONE_MOBILE }, new int[] { R.id.name,
                        R.id.email, R.id.mobile });

        setListAdapter(adapter);
    }

}   

}
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
 ArrayList<HashMap<String, String>> contactList;
contactList = new ArrayList<HashMap<String, String>>();