0
  • 我得到一些編譯時錯誤,而延伸到 BaseFragment。如果我致以VideoDetailFragment.javaActivity我沒有得到任何 錯誤。未定義的方法或構造錯誤而延伸到BaseFragment

  • 但我需要把它擴大到BaseFragment。因爲我需要得到 ListView同時點擊我可以得到在 網址顯示的視頻。

issue是我收到以下錯誤:

  1. 構造ArrayAdapter(VideoDetailFragment,INT的String [])是不確定的
  2. 的方法完成()是未定義的類型VideoDetailFragment
  3. 構造意圖(VideoDetailFragment, 類)是未定義
  4. 構造ProgressDialog(VideoDetailFragment )是未定義的

下面我提到了這些行結束時的所有錯誤。

VideoDetailFragment.java:

package com.sit.fth.frgment; 

import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.app.ProgressDialog; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import com.fth.android.R; 
import com.sit.fth.app.BaseFragment; 
import com.sit.fth.model.JSONParser; 
import com.sit.fth.activity.YoutubePlayActivity; 

import java.util.ArrayList; 
import java.util.List; 


public class VideoDetailFragment extends BaseFragment { 
    // Categories must be pre-set 
    private String[] data = {"Category1", "Category2", "Category3"}; 
    private final String TAG_VIDEOS = "videos"; 
    private final String TAG_CAT = "video_category"; 
    private final String TAG_URL = "video_url"; 
    private final String TAG_TITLE = "video_title"; 



    private List<String> videoTitles = new ArrayList<String>(); 
    private List<String> videoURLs = new ArrayList<String>(); 
    private ArrayAdapter<String> adapter; 
    private Object extras; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.list1, null); 




     ListView listView = ((ListView)view.findViewById(R.id.listview)); 


     AdapterView.OnItemClickListener clickListener = null; 

     // Category view: 
     if (extras == null) { 
      clickListener = new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        Intent intent = new Intent(VideoDetailFragment.this,VideoDetailFragment.class); ----->3rd Error 
        intent.putExtra("categoryName", data[position]); 
        startActivity(intent); 
       } 
      }; 


      adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, data); --->1st Error at this line 
     } 
     else { // Child view 
      // Get the category of this child 
      String categoryName = ((Bundle) extras).getString("categoryName"); 
      if (categoryName == null) 

       finish();  ------>2nd Error 

      // Populate list with videos of "categoryName", by looping JSON data 
      new JSONParse(categoryName).execute(); 

      clickListener = new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
        Intent intent = new Intent(VideoDetailFragment.this, YoutubePlayActivity.class); ----->Third Error 
        // Send video url and title to YoutubeActivity 
        intent.putExtra("videoUrl", videoURLs.get(position)); 
        intent.putExtra("videoTitle", videoTitles.get(position)); 
        startActivity(intent); 
       } 
      }; 
      adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, videoTitles); ---->First Error 
     } 

     listView.setAdapter(adapter); 
     listView.setTextFilterEnabled(true); 
     listView.setOnItemClickListener(clickListener); 
     return listView; 
    } 





    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     private String categoryName; 

     // Constructor // Get the categoryName of which videos will be found 
     public JSONParse(String category) { 
      this.categoryName = category; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Create a loading dialog when getting the videos 
      pDialog = new ProgressDialog(VideoDetailFragment.this); ---->4th Error 
      pDialog.setMessage("Getting Videos..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     @Override 
     protected JSONObject doInBackground(String... args) { 
      JSONParser jParser = new JSONParser(); 
      // Get JSON from URL 
      JSONObject json = jParser.getJSONFromUrl(JSONUrl); 
      if (json == null) 
       return null; 

      try { 
       // Get video array 
       JSONArray videos = json.getJSONArray(TAG_VIDEOS); 

       // Loop all videos 
       for (int i=0; i<videos.length(); i++) { 
        JSONObject video = videos.getJSONObject(i); 
        Log.e("JSON:", "cat: "+video.getString(TAG_CAT)+",title: "+video.getString(TAG_TITLE)+", url: "+video.getString(TAG_URL)); 
        // Check if video belongs to "categoryName" 
        if (video.getString(TAG_CAT).equals(categoryName)) { 
         addVideo(video); 
        } 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return json; 
     } 

     private void addVideo(JSONObject video) { 
      try { 
       // Add title and URL to their respective arrays 
       videoTitles.add(video.getString(TAG_TITLE)); 
       videoURLs.add(video.getString(TAG_URL)); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     protected void onPostExecute(JSONObject json) { 
      // Close the "loading" dialog 
      pDialog.dismiss(); 
      if (json == null) { 
       // Do something when there's no internet connection 
       // Or there are no videos to be displayed 
      } 
      else // Let the adapter notify ListView that it has new items 
       adapter.notifyDataSetChanged(); 
     } 
    } 


} 

我不知道如何解決these.Anybody可以幫我these.thank你。

回答

1

您需要提供對Context的引用,而不是片段。而不是adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, videoTitles),你應該做adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, videoTitles);,同樣在getActivity()而不是this的所有其他方法,你有erros。

+0

你的答案也對我很有幫助。 – Steve

1

片段不是上下文對象。您需要通過使用getActivity()作爲數組適配器中的第一個參數來傳遞Activity對象。

Instead of passing this in this line ==> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);

通getActivity()這樣的:

adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, data);

+0

沒有,如果我擴展活動我沒有得到一個窗口listview – Steve