在片段内执行方法

时间:2015-09-08 17:01:11

标签: android android-fragments

我在尝试在Fragment中执行方法时收到警告。

public class PrimaryFragmentDormir extends Fragment {

    // Declare Variables
    ListView listview;
    List<ParseObject> ob;
    ProgressDialog mProgressDialog;
    ListViewAdapter adapter;
    private List<WorldPopulation> worldpopulationlist = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.primary_layout_dormir,null);

        // Get the view from listview_main.xml
       // setContentView(R.layout.listview_main);
        // Execute RemoteDataTask AsyncTask
issue here==>        new RemoteDataTask.execute();
        //test commit dell
    }

    // RemoteDataTask AsyncTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(getActivity());
            // Set progressdialog title
            mProgressDialog.setTitle("Parse.com Custom ListView Tutorial");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            worldpopulationlist = new ArrayList<WorldPopulation>();
            try {
                // Locate the class table named "Country" in Parse.com
                ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                        "Country");
                // Locate the column named "ranknum" in Parse.com and order list
                // by ascending
                query.orderByAscending("ranknum");
                ob = query.find();
                for (ParseObject country : ob) {
                    // Locate images in flag column
                    ParseFile image = (ParseFile) country.get("flag");

                    WorldPopulation map = new WorldPopulation();
                    map.setRank((String) country.get("rank"));
                    map.setCountry((String) country.get("country"));
                    map.setPopulation((String) country.get("population"));
                    map.setFlag(image.getUrl());
                    worldpopulationlist.add(map);
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // Locate the listview in listview_main.xml
            listview = (ListView) getView().findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(PrimaryFragmentDormir.this.getActivity(),
                    worldpopulationlist);
            // Binds the Adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();
        }
    }

    }

我已经找到了解决方案,但我无法解决问题。

谢谢

2 个答案:

答案 0 :(得分:1)

是我还是你的代码无法编译,因为行

new RemoteDataTask.execute();

无法联系到?回来后就是这样。

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.primary_layout_dormir,null);
    // ...
    new RemoteDataTask.execute();
}

其次,如上所述,您需要括号来创建新对象:

new RemoteDataTask().execute();

尝试这样的事情:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    new RemoteDataTask().execute();
    return inflater.inflate(R.layout.primary_layout_dormir,null);
}

答案 1 :(得分:1)

你的语法有点偏离:

new RemoteDataTask.execute();

使用new实例化对象时,需要括号如下:new RemoteDataTask()

您的错误显示“无法解析符号'execute'”,因为execute()类上的RemoteDataTask方法不作为静态方法存在,即使它已经存在,也无效使用new运算符。

因此,您希望将该行更改为:

new RemoteDataTask().execute();