0

我正在开发我的应用程序,并且我的片段中有 ProgressDialog 方法,出于某种原因,它说我传递给 ProgressDialog 的活动是未定义的。对我来说,它没有产生,因为我使用了活动名称。这应该只是说你指向当前的活动,对吗?我究竟做错了什么?

Here is the errors i am getting
No enclosing instance of the type MainActivity is accessible in scope   FragmentHome.java   /SideMenuTabs/src/com/begin/sidemenutabs    line 98 Java Problem

The constructor ProgressDialog(FragmentHome.DownloadJSON) is undefined  FragmentHome.java   /SideMenuTabs/src/com/begin/sidemenutabs    line 54 Java Problem

The method findViewById(int) is undefined for the type FragmentHome.DownloadJSON    FragmentHome.java   /SideMenuTabs/src/com/begin/sidemenutabs    line 96 Java Problem

说未定义的部分是。

第 5 行在此。

protected void onPreExecute() {
    super.onPreExecute();
    // Create a progressdialog
    //Undefined error
    mProgressDialog = new ProgressDialog(FragmentHome.this);
    // Set progressdialog title
    mProgressDialog.setTitle("Testing Parse 1");
    // Set progressdialog message
    mProgressDialog.setMessage("Loading...");
    mProgressDialog.setIndeterminate(false);
    // Show progressdialog
    mProgressDialog.show();
}

还有第 6 行。

    protected void onPostExecute(Void args) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        //Undefined error
        adapter = new ListViewAdapter(MainActivity.this, arraylist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }

这是我的整个片段。

    public class FragmentHome extends SherlockFragment {

        private final String TAG = "Home";
        // Parse Declare Variables
        JSONObject jsonobject;
        JSONArray jsonarray;
        ListView listview;
        ListViewAdapter adapter;
        ProgressDialog mProgressDialog;
        ArrayList<HashMap<String, String>> arraylist;
        static String RANK = "rank";
        static String COUNTRY = "country";
        static String POPULATION = "population";
        static String FLAG = "flag";


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.i(TAG, "Home Loaded");
            View rootView = inflater.inflate(R.layout.fragmenthome, container, false);
            new DownloadJSON().execute();
            return rootView;

        }

// DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(FragmentHome.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Testing Parse 1");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
        }



    @Override
        protected Void doInBackground(Void... params) {
            // Create the array 
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrive JSON Objects from the given website URL in JSONfunctions.class
            jsonobject = JSONfunctions.getJSONfromURL("parsingurl");

            try {
                // Locate the array name
                jsonarray = jsonobject.getJSONArray("worldpopulation");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    map.put("rank", jsonobject.getString("rank"));
                    map.put("country", jsonobject.getString("country"));
                    map.put("population", jsonobject.getString("population"));
                    map.put("flag", jsonobject.getString("flag"));
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

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

    }
4

3 回答 3

2

AnyClass.this指向AnyClass当前范围内的实例。这意味着您必须要么在类本身的实例方法中,要么在此类的实例类中。如果您在 之外AnyClass,则无法访问它。

从 aFragment中,您需要调用getActivity()以获取Activity.

于 2013-08-12T16:21:01.447 回答
0

试试 progressDialog = new ProgressDialog((MainActivity)getActivity()); 如果您从 Fragment 调用 progressBar

MainActivity 是您的活动

于 2013-11-27T04:56:32.530 回答
0

对于 progressDialog() 试试这个...

pDialog = new ProgressDialog(getContext());
于 2015-09-18T07:54:06.307 回答