我是Android新手开发的新手。我正在处理我的应用程序,我在我的片段中有ProgressDialog方法,由于某种原因,它说我传递给ProgressDialog的活动是未定义的。对我来说它没有成功,因为我使用activityname.this应该只说你指向当前活动,我是否正确?我做错了什么?
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();
}
}
}
提前致谢。
答案 0 :(得分:2)
AnyClass.this
指向当前范围中的AnyClass
实例。这意味着您必须要么在类本身的实例方法中,要么在此类的实例类中。如果您不在AnyClass
之内,则无法访问它。
从Fragment
,您需要致电getActivity()
以获取Activity
的实例。
答案 1 :(得分:0)
尝试progressDialog = new ProgressDialog((MainActivity)getActivity());如果你从片段中调用progressBar
MainActivity是您的活动
答案 2 :(得分:0)
for progressDialog()试试这个...
pDialog = new ProgressDialog(getContext());