我正在开发我的应用程序,并且我的片段中有 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();
}
}
}