2016-11-08 121 views
0

我正在關注android's website中的Android培訓。我被困在某些片段中。問題是我無法在片段中獲得對textview的引用。getActivity()。findViewById(int id)即使在片段完成構建後也會返回null

TextView view = (TextView) getActivity().findViewById(R.id.article_s); 

我在一個活動中有兩個片段。一個是簡單的簡單ListFragment,另一個是簡單的簡單TextView。這裏是片段和活動佈局xml文件。

ArticleFragment:

<!-- This is fragment_article.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Some Text" 
    android:textSize="18sp" 
    android:id="@+id/article_s"/> 

MainActivity(我的容器的所有片段)

<!-- This is activity_main.xml --> 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<fragment 
    android:layout_height="match_parent" 
    android:name="com.example.bora.fragmentsmyway.HeadlinesFragment" 
    android:id="@+id/headlines_fragment" 
    android:layout_weight="1" 
    android:layout_width="0dp" /> 

<fragment 
    android:layout_height="match_parent" 
    android:name="com.example.bora.fragmentsmyway.ArticleFragment" 
    android:layout_weight="2" 
    android:id="@+id/article_fragment" 
    android:layout_width="0dp" /> 

</LinearLayout> 

有一個在我的ArticleFragment公共方法,它需要一個字符串來更新自身的TextView的內容:

public void updateView(String article) { 
     TextView view = (TextView) getActivity().findViewById(R.id.article_s); 
     try{ 
      view.setText(article); 
     } catch (NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 

在創建所有視圖後,我打電話給updateView從我的MainActivity方法。但是當我試圖找到文章TextView時,我得到了一個NullPointerException。我已經嘗試清理,更改TextView的資源ID。

但是我找到了解決這個問題的辦法。我不知道爲什麼,但它似乎當我在fragment_article.xmlLinearLayout包裹TextView工作:

<LinearLayout 
    xmlns:android="..." 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some Text" 
     android:textSize="18sp" 
     android:id="@+id/article_s" /> 

</LinearLayout> 

但我想知道這一點:哪有,教程樣本中,上面的代碼工作,不能我寫作時的工作?我通過信件檢查了每個字母,但我的螞蟻之間沒有區別。是因爲版本?

我正在使用API​​ 23,它是棉花糖。

+0

只更改爲R.id.article到R.id.article_s –

+0

**我不知道爲什麼,但它似乎工作時,我用fragment_article.xml中的LinearLayout包裝TextView。你能告訴我們的代碼? – Raghunandan

+0

@Raghunandan我已添加。 – Bora

回答

2

確保您的片段連接到活動呼叫updateView

在此之前

TextView view = (TextView) getActivity().findViewById(R.id.article_s); 

會給你NPE導致它不屬於活動

的意見屬於片段在這種情況下你做

View view = inflater.inflate(R.layout.fragment_layout, container, false); 
TextView textView = (TextView) view.findViewById(R.id.article_s); 

具體來說,片段可與getActivity訪問活動實例(),並輕鬆地執行任務,如發現在活動佈局視圖:

閱讀的活動交流@https://developer.android.com/guide/components/fragments.html

所以你使用getActivity找到屬於意見到代碼中並非如此的活動。

編輯 -

LinearLayout結束語它不應該有任何區別,因爲它只是一個ViewGroup中。如果你只需要一個textview不需要LinearLayout

在閱讀接受的答案:Difference between getActivity() and view in Fragment

1

使用此代碼:你的TextView

TextView view = (TextView) getActivity().findViewById(R.id.article_s); 
0

給出錯誤的ID來這是回報NPE因爲你試圖找到錯誤的ID。 有兩種不同的方法可以解決問題。

1)首先一個

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Some Text" 
    android:textSize="18sp" 
    android:id="@+id/article"/> 

TextView view = (TextView) getActivity().findViewById(R.id.article); 

另一種方式讓你的XML,因爲它是&嘗試做如下改變Java代碼。

2)第二個

<TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Some Text" 
     android:textSize="18sp" 
     android:id="@+id/article_s"/> 

TextView view = (TextView) getActivity().findViewById(R.id.article_s); 
0

它沒有這樣的,你應該上傳您的主要活動代碼或您的片段類的方式,如果你正在使用內它的工作片段類中主要活動,你不需要使用getActivity()

,如果它是一個片段類中,你需要這樣的應用它:

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

     View rootView = inflater.inflate(R.layout.your_fragment_layout_xml, container, false); 
     TextView AboutUs = (TextView) rootView.findViewById(R.id.id_of_textview); 
相關問題