2016-11-08 10 views
0

android's websiteのアンドロイドトレーニングに従っています。私は断片的なところで固まっている。問題は、断片化したテキストビューへの参照を取得できないことです。getActivity()。フラグメントがビルドを完了してもfindViewById(int id)がnullを返します

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

1つのアクティビティに2つのフラグメントがあります。 1つはプレーンな単純なListFragmentであり、もう1つはプレーンな単純な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> 

自体ののTextViewの内容を更新するために、文字列を受け取り、私のArticleFragmentの公共の方法があります:

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

すべてのビューが作成された後、私は私のMainActivityのupdateViewメソッド。しかし、記事TextViewを見つけようとするとNullPointerExceptionが出ます。 TextViewのリソースIDを変更してクリーニングを試みました。

しかし、私はこの問題の解決策を見つけました。私はなぜ知らないが、私はfragment_article.xmlLinearLayoutTextViewを包むときに動作するようです:

<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> 

しかし、私はこの疑問に思って:どのように、チュートリアルのサンプルでは、​​コードの作業上のとできませんの私が書いたときに働く?私はすべてのコードを手紙でチェックしましたが、私のアリの違いはありません。それはバージョンのためですか?

私はmarshmallowであるAPI 23を使用しています。

+0

R.id.articleからR.id.article_sに変更する –

+0

**私は理由はわかりませんが、Text_Viewをfragment_article.xml **内のLinearLayoutでラップすると動作するようです。コードを見せてくれますか? – 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を使用あなたのコードではそうではありません。

編集 - それだけのViewGroupあるよう

LinearLayoutでそれをラップは、任意の違いを作るべきではありません。単一のものが必要な場合はtextviewLinearLayoutの必要はありません。

で受け入れ答えを読む:Difference between getActivity() and view in Fragment

1

使用このコード:あなたの試みは間違ったIDを検索しているため、あなたがTextViewの

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

上に間違ったIDを与えられたこれは、戻りNPEです。 問題を解決するには2通りの方法があります。もう一つの方法は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); 

1)まず、それはJavaコードに変更を下回るやろう&であるとして、あなたのXMLを保持します。

2)第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

あなたの中にそれを使用している場合、それはあなたのメインのアクティビティコードをアップロードする必要がある方法か、フラグメント・クラスによってフラグメントクラス内のように動作しません。

@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); 
0123:それはあなたがこのようにそれを適用する必要がありフラグメントのクラス内にある場合主な活動は、あなたが getActivity()

を使用する必要はありません

関連する問題