2013-08-12 3 views
0

나는 안드로이드 개발에 처음 왔어. 내 애플 리케이션에서 일하고 있는데 내 조각 안에 ProgressDialog 메서드가 있고 어떤 이유로 ProgressDialog에 전달할 액티비티가 정의되지 않았다고 말합니다. 나에게 그것은 현재 활동을 가리키고 있다고 말할 수있는 activityname.this를 사용하기 때문에 그다지 나을 수 없다. 맞습니까? 내가 도대체 ​​뭘 잘못하고있는 겁니까?정의되지 않은 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(); 
     } 
    } 

    } 

미리 감사드립니다.

+0

그것을 편집하여 ** 실제 ** 스택 추적 및 오류 메시지 – njzk2

+0

를 게시 죄송합니다. 오류 코드를 추가했습니다. – Suzed

답변

2

AnyClass.this은 현재 범위의 AnyClass 인스턴스를 가리 킵니다. 즉, 클래스 자체의 인스턴스 메서드 또는이 클래스의 인스턴스 클래스에 있어야합니다. AnyClass 밖에 있으면 액세스 할 수 없습니다.

Fragment에서 Activity의 인스턴스를 얻으려면 getActivity()으로 전화해야합니다.

0

시도 progressDialog = 새 ProgressDialog ((MainActivity) getActivity()); 당신이 조각에서의 ProgressBar를 호출하는 경우 MainActivity이 활동

해서 ProgressDialog에 대한
0

()이 시도입니다

...

pDialog = new ProgressDialog(getContext()); 
관련 문제