2016-10-01 6 views
0

タブを使用しようとしましたが、PagerAdapterはタブレイアウトを作成しようとしています。ビューにアクセスする際のフラグメントエラー

I持って私のFragmentに次の式:次のエラーで

cpPrecTextView = (BootstrapLabel) getView().findViewById(R.id.cpPrec); 
cpPrecTextView.setText(cpPrecTextView.getText() + cpPrec); 

マイアプリのクラッシュ:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 
         at com.pgoiv.pokemongoiv.TabFragment1.onCreateView(TabFragment1.java:45) 

何が悪いのでしょうか?

+0

は、 'getView()'がnullであることを示します。 –

答えて

0

getView()はnullを返します。 Fragmentのコンテナビューを適切に膨張させなかったと仮定しています。したがって、あなたのonCreateViewの中では、まずこのようにビューを膨張させる必要があります。

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

    // Inflate the layout and get the reference of the view 
    View v = inflater.inflate(R.layout.fragment_my_tickets, container, false); 

    // Now find any view inside that 
    cpPrecTextView = (BootstrapLabel) v.findViewById(R.id.cpPrec); 
    cpPrecTextView.setText(cpPrecTextView.getText() + cpPrec); 

    return v; 
} 
0

getView()リターンはあなたがonCreateViewでビューに依存するすべてのコードを実行する必要があり、これを解決するために

をゼロ。そうすることで、他のビューを見つけるためのビューを常に参照することができます。

フラグメントライフサイクルの詳細については、Android Developers pageを参照してください。

関連する問題