ListView在Fragment中不起作用

时间:2014-04-27 15:39:45

标签: java android fragment

列表视图在Fragment中不起作用。

Fragment.java

public class Fragment_Main extends Fragment{
ArrayAdapter<String> categoryAdapter;
ArrayList<String> categoryList = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
     View View = inflater.inflate(R.layout.fragment_main, container, false);
     ListView list = (ListView) getView().findViewById(R.id.listView1);
     categoryAdapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, categoryList);
    list.setAdapter(categoryAdapter);
    showCategoryTask task=new showCategoryTask();
    task.execute();
        return View;
 }

 class showCategoryTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            try {
                URL url = new URL("http://garyhui86.er-webs.com/index.xml");
                HttpURLConnection urlConn = 
                                     (HttpURLConnection)url.openConnection();
                if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    DocumentBuilder builder = DocumentBuilderFactory
                            .newInstance().newDocumentBuilder();
                    Document document = builder.parse(urlConn.getInputStream());
                    NodeList nodeList = document.getElementsByTagName("Info");
                    for (int i = 0; i < nodeList.getLength(); i++) {
                        NamedNodeMap attributes=nodeList.item(i).getAttributes();
                        String category=attributes.getNamedItem("categoryName").getNodeValue();
                        Log.i("ttt", category);
                        categoryList.add(category);
                    }
                }
                urlConn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            categoryAdapter.notifyDataSetChanged();
        }
    }
}
Error Log:
04-27 15:39:02.568: E/AndroidRuntime(7826): FATAL EXCEPTION: main
04-27 15:39:02.568: E/AndroidRuntime(7826): java.lang.RuntimeException: Unable to start activity ComponentInfo{hkpro.shopdada.com.summonboardinfo/hkpro.shopdada.com.summonboardinfo.MainActivity}: java.lang.NullPointerException
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.os.Looper.loop(Looper.java:137)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at dalvik.system.NativeStart.main(Native Method)
04-27 15:39:02.568: E/AndroidRuntime(7826): Caused by: java.lang.NullPointerException
04-27 15:39:02.568: E/AndroidRuntime(7826):     at hkpro.shopdada.com.summonboardinfo.Fragment_Main.onCreateView(Fragment_Main.java:39)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.Activity.performStart(Activity.java:5114)
04-27 15:39:02.568: E/AndroidRuntime(7826):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
04-27 15:39:02.568: E/AndroidRuntime(7826):     ... 11 more

2 个答案:

答案 0 :(得分:1)

使用膨胀的对象查找&#39; ListView&#39;。

这样做:

&#39; ListView list =(ListView)inflated_layout_object.findViewById(R.id.listView1);&#39;

这里为你inflated_layout_object是View(你已创建)。

答案 1 :(得分:0)

我想这行会造成麻烦:

 View View = inflater.inflate(R.layout.fragment_main, container, false);
 ListView list = (ListView) getView().findViewById(R.id.listView1);

请勿使用getView()方法按ID查找视图。改为使用膨胀的布局:

 View root = inflater.inflate(R.layout.fragment_main, container, false);
 ListView list = (ListView) root.findViewById(R.id.listView1);

原因是getView()仅在onCreateView返回后才返回片段视图。