缺少返回语句Android



我在Fragment中实现了ListView。现在我在之后得到错误'missing return statement'

new GetSloten().execute();
 }

需要帮助

这是我的代码:

public class SlotenFragment extends ListFragment {
private ProgressDialog nDialog;
// URL to get contacts JSON
private static String url = "http://charlenemacdonald.com/sloten.json";
// JSON Node names
private static final String TAG_SLOTEN = "slotenlijst";
private static final String TAG_SLOT = "Slot";

// contacts JSONArray
JSONArray sloten= null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> slotenLijst;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    slotenLijst = new ArrayList<HashMap<String, String>>();
    ListView lv = (ListView) getView().findViewById(android.R.id.list);
    // Listview on item click listener
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            String Slot = ((TextView) view.findViewById(R.id.textviewslotnaam))
                    .getText().toString();

            // Starting single contact activity
            Intent in = new Intent(getActivity().getApplicationContext(),
                    SlotInfoScherm1.class);
            in.putExtra(TAG_SLOT, Slot);
            startActivity(in);
        }
    });
    // Calling async task to get json
    new GetSloten().execute();
}
/**
 * Async task class to get json by making HTTP call
 * */
private class GetSloten extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        nDialog = new ProgressDialog(getActivity());
        nDialog.setMessage("Even geduld a.u.b., studenten worden geladen...");
        nDialog.setCancelable(false);
        nDialog.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
                sloten = jsonObj.getJSONArray(TAG_SLOTEN);
                // looping through All Contacts
                for (int i = 0; i < sloten.length(); i++) {
                    JSONObject c = sloten.getJSONObject(i);
                    String Slot = c.getString(TAG_SLOT);

                    // tmp hashmap for single contact
                    HashMap<String, String> sloten = new HashMap<String, String>();
                    // adding each child node to HashMap key => value
                    sloten.put(TAG_SLOT, Slot);

                    // adding contact to contact list
                    slotenLijst.add(sloten);
                }
            } 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 (nDialog.isShowing())
            nDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                SlotenFragment.this, slotenLijst,
                R.layout.sloten_info, new String[] { TAG_SLOT}, new int[] { R.id.textviewslotnaam});
        setListAdapter(adapter);
    }
}

    View rootView = inflater.inflate(R.layout.fragment_sloten, container, false);
    return rootView;

}
}

提前感谢!

通常,您应该在onCreateView()中展开view,从中查找所有感兴趣的视图,并在方法结束时返回它。

类似这样的东西:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.fragment_layout, container, false);
   ListView lv = (ListView) view.findViewById(R.id.list);
   // .......
   return view;
}

你是这样做的:

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

如果是,则可以返回lv

但我怀疑这是正确的方式,因为getView()返回片段布局的根视图,而您没有对其进行膨胀。如果您返回lv,我可以在那行看到一个NullPointerException

方法public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)要求返回一个View对象。你当然应该返回你的碎片的视图如下
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.your_layout, container, false);
    ListView lv = (ListView) view.findViewById(android.R.id.list);
    // Listview on item click listener
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
            // getting values from selected ListItem
            String Slot = ((TextView) view.findViewById(R.id.textviewslotnaam))
                .getText().toString();

            // Starting single contact activity
            Intent in = new Intent(getActivity().getApplicationContext(),
                SlotInfoScherm1.class);
            in.putExtra(TAG_SLOT, Slot);
            startActivity(in);
        }
    });
    //Return your view
    return view;
}