2015-09-08 62 views
-1

我在嘗試執行片段內部的方法時收到警告。在片段內部執行方法

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(); 
     } 
    } 

    } 

我已經搜索了一個解決方案,但我無法解決問題。

謝謝

+0

什麼是警告說什麼?這應該指向你的問題。 –

+0

@ Tanis.7x,警告說:無法解析符號'執行' – mvasco

+0

什麼警告? ()()()()()()()請使用new操作符刪除()()()()()()()請刪除onCreateView中的對話框,並且不應該在onCreateView中顯示對話框有更多更好的片段的生命週期回調... – Selvin

回答

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); 
} 
+0

謝謝你,我改變了你的建議,也沒有成功。 – mvasco

+0

我剛剛編輯了你應該做的代碼。 –

+0

謝謝你現在正在工作 – mvasco

1

你的語法是有點偏離這裏:

new RemoteDataTask.execute(); 

當你用new實例化一個對象,你需要像這樣的括號:new RemoteDataTask()

你的錯誤說「無法解析符號‘執行’」,因爲​​方法不作爲對RemoteDataTask類的靜態方法存在,即使它不會與new運營工作。

因此,你要到該行更改爲:

new RemoteDataTask().execute(); 
+0

謝謝你,我改變了你的建議,也沒有成功。 – mvasco

+0

由於在你得到完全相同的錯誤信息? –

+0

不,現在它說:無法到達 – mvasco