0

我想添加自定義列表視圖到擴展片段的類。但它在4個地方給出了錯誤。下面是代碼:添加列表視圖到片段給出錯誤

TopStoriesFragment.java

public class TopStoriesFragment extends Fragment { 

    public String thehindu = "http://www.thehindu.com/news/?service=rss"; 
    public String toi = "http://timesofindia.feedsportal.com/c/33039/f/533965/index.rss"; 
    public String reuters = "http://feeds.reuters.com/reuters/topNews"; 
    public String deccanherald = "http://www.deccanherald.com/rss-internal/top-stories.rss"; 
    public String currenturl = thehindu; 
    AlertDialog levelDialog; 
    public String fakingnews = "http://thepuntended.wordpress.com/"; 

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

     View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false); 

     feedTitle = (TextView) rootView.findViewById(R.id.feedtitle); 
     feedDescribtion = (TextView) rootView.findViewById(R.id.feeddescribtion); 

     startReadRss(); 

     return rootView; 
    } 

    public class RssLoadingTask extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      displayRss(); 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      preReadRss(); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      // TODO Auto-generated method stub 
      // super.onProgressUpdate(values); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // TODO Auto-generated method stub 
      readRss(); 
      return null; 
     } 

    } 

    private RSSFeed myRssFeed = null; 

    TextView feedTitle; 
    TextView feedDescribtion; 

    // TextView feedPubdate; 
    // TextView feedLink; 

    public class MyCustomAdapter extends ArrayAdapter<RSSItem> { 

     public MyCustomAdapter(Context context, int textViewResourceId, 
       List<RSSItem> list) { 
      super(context, textViewResourceId, list); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      // return super.getView(position, convertView, parent); 

      View row = convertView; 
      if (row == null) { 

    //------------------------------- Error 1------------------------------- 
       LayoutInflater inflater = getLayoutInflater(); 
    //------------------------------- Error 1------------------------------- 

       row = inflater.inflate(R.layout.row, parent, false); 
      } 

      TextView listTitle = (TextView) row.findViewById(R.id.listtitle); 
      listTitle.setText(myRssFeed.getList().get(position).getTitle()); 
      TextView listPubdate = (TextView) row 
        .findViewById(R.id.listpubdate); 
      listPubdate.setText(myRssFeed.getList().get(position).getPubdate()); 

      if (position % 2 == 0) { 
       listTitle.setBackgroundColor(0xff101010); 
       listPubdate.setBackgroundColor(0xff101010); 
      } else { 
       listTitle.setBackgroundColor(0xff080808); 
       listPubdate.setBackgroundColor(0xff080808); 
      } 

      return row; 
     } 
    } 

    private void startReadRss() { 
     new RssLoadingTask().execute(); 
    } 

    private void preReadRss() { 
     feedTitle.setText("--- wait ---"); 
     feedDescribtion.setText(""); 
     // feedPubdate.setText(""); 
     // feedLink.setText(""); 

    //------------------------------- Error 2 (Solved)------------------------------- 
     setListAdapter(null); 
    //------------------------------- Error 2------------------------------- 

     Toast.makeText(getActivity(), "Reading RSS, Please wait.", Toast.LENGTH_LONG) 
       .show(); 
    } 

    private void readRss() { 

     try { 
      URL rssUrl = new URL(currenturl); 
      SAXParserFactory mySAXParserFactory = SAXParserFactory 
        .newInstance(); 
      SAXParser mySAXParser = mySAXParserFactory.newSAXParser(); 
      XMLReader myXMLReader = mySAXParser.getXMLReader(); 
      RSSHandler myRSSHandler = new RSSHandler(); 
      myXMLReader.setContentHandler(myRSSHandler); 
      InputSource myInputSource = new InputSource(rssUrl.openStream()); 
      myXMLReader.parse(myInputSource); 

      myRssFeed = myRSSHandler.getFeed(); 

     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ParserConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SAXException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    private void displayRss() { 
     if (myRssFeed != null) { 
      Calendar c = Calendar.getInstance(); 
      // String strCurrentTiime = "\n(Time of Reading - " 
      // + c.get(Calendar.HOUR_OF_DAY) 
      // + " Hrs. : " 
      // + c.get(Calendar.MINUTE) + " Mins.)\n" +c.get(Calendar.AM_PM); 

      SimpleDateFormat sdf = new SimpleDateFormat(
        "MMMM-dd-yyyy HH:mm:ss a"); 
      String strCurrentTiime = sdf.format(c.getTime()); 

      feedTitle.setText(myRssFeed.getTitle()); 
      feedDescribtion.setText(strCurrentTiime); 
      // feedPubdate.setText(myRssFeed.getPubdate()); 
      // feedLink.setText(myRssFeed.getLink()); 

      MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), R.layout.row, 
        myRssFeed.getList()); 

    //------------------------------- Error 3 (Solved)------------------------------- 
      setListAdapter(adapter); 
    //------------------------------- Error 3------------------------------- 

     } 
    } 

    @Override 
    //------------------------------- Error 4 (Solved)------------------------------- 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    //------------------------------- Error 4------------------------------- 
     // TODO Auto-generated method stub 
     Intent intent = new Intent(getActivity(), ShowDetails.class); 
     Bundle bundle = new Bundle(); 
     bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle()); 
     bundle.putString("keyDescription", myRssFeed.getItem(position) 
       .getDescription()); 
     bundle.putString("keyLink", myRssFeed.getItem(position).getLink()); 
     bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate()); 
     intent.putExtras(bundle); 
     startActivity(intent); 
    } 
} 

Top_rated fragment.xml之

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/back" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/feedtitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/Black" 
     android:layout_gravity="center" 
     android:layout_marginTop="10dp" 
     android:textStyle="bold" /> 

    <TextView 
     android:textColor="@color/Black" 
     android:id="@+id/feeddescribtion" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 

    </LinearLayout> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:divider="@drawable/divider" /> 

    <TextView 
     android:id="@android:id/empty" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="No Data" /> 

</LinearLayout> 

的4個地方有錯誤的字裏行間標記英寸錯誤是:

** 1:在類型片段的方法getLayoutInflater(束)是不適用的參數()**

2:該方法setListAdapter(空)是未定義的類型TopStoriesFragment (由代替延伸片段的Lis​​tFragment解決。)

3:(通過擴展,而不是片段ListFragment解決)的方法setListAdapter(TopStoriesFragment.MyCustomAdapter)是未定義的類型TopStoriesFragment

4:該方法onListItemClick(ListView控件,查看,的int,long)型TopStoriesFragment必須覆蓋或實現超方法(通過增加公衆對方法解決)

+0

錯誤似乎是不言自明的。你有什麼問題? –

+0

我不能擴展ListFragment,我只能擴展Fragment。這樣做會導致錯誤1,2和4再次出現。請幫助我更多.. – Akshat

+0

您的佈局(fragment_top_rated.xml)是否具有「ListView」作爲佈局的一部分? –

回答

2

你的問題似乎可以通過擴展ListFragment而不是Fragment來解決,但你說你不能這樣做。因此,讓我們把你的問題,一次一個:

1:在類型片段的方法getLayoutInflater(捆綁)不適用()的參數

正如@ G3rcar建議,你可以聲明一個LayoutInflater成員字段,將其初始化爲LayoutInflater傳遞給onCreateView方法,並在後面的代碼中使用該字段。或者,可以使用內MyCustomAdapater.getView()檢索LayoutInflater隨時:

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
    Context.LAYOUT_INFLATER_SERVICE); 

2:該方法setListAdapter(空)是未定義的類型TopStoriesFragment(通過擴展,而不是片段ListFragment解決。)

從你的代碼判斷,你可以簡單地消除這個調用。另外,請參閱我對下一個問題的建議。

3:(通過擴展,而不是片段ListFragment解決)的方法setListAdapter(TopStoriesFragment.MyCustomAdapter)是未定義的類型TopStoriesFragment

該溶液到兩個這個問題,並與前一個是將其存儲到ListView參考當您第一次構建片段的觀點:

TextView feedTitle; 
TextView feedDescribtion; 
ListView listView; 

public View onCreateView(...) { 
    ... 
    feedTitle = (TextView) rootView.findViewById(R.id.feedtitle); 
    feedDescribtion = (TextView) rootView.findViewById(R.id.feeddescribtion); 
    listView = (ListView) rootView.findViewById(android.R.id.list); 
    ... 
} 

然後你可以設置爲012適配器直接:

private void displayRss() { 
    ... 
    listView.setAdapter(adapter); // instead of "setAdapter(adapter)" 
} 

4:類型TopStoriesFragment的方法onListItemClick(ListView中,查看,整型,長)必須重寫或實現的超類型方法

(通過添加公共的方法來解決)

您可以明確地設置一個OnItemClickListenerListView對項目作出迴應點擊:

listView = (ListView) rootView.findViewById(android.R.id.list); 
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     ... // body of your current onListItemClick() method 
    } 
}); 

然後,您將從TopStoriesFragment中刪除方法onListItemClick

+0

非常感謝。它解決了所有的錯誤。但是我現在正面臨一個空指針異常的新問題。請參閱[http://stackoverflow.com/questions/24538957/null-pointer-exception-on-layout-inflator](this)問題。 – Akshat

2

希望我可以幫你的。

1. getLayoutInflater()方法是給你的錯誤,因爲你必須從德onCreateView方法保存充氣的實例

public LayoutInflater inflater 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    this.inflater = inflater; 
} 

然後,你必須使用它從實例,而不是使用getLayoutInflater()

另一種方式是將充氣器作爲參數傳遞給MyCustomAdapter構造函數。

+0

是的..它的工作。但我有一個新問題,我不能擴展ListFragment,我只能擴展Fragment。這樣做會導致錯誤1,2和4再次出現。你能幫忙嗎?? +1和這個答案很多thanx ..請幫助我更多.. – Akshat