0

Java에서 LinearLayout에 TextView를 추가하려고하지만 LinearLayout linlay_containermake_systematics()에 액세스하자 마자 NullPointerException이 발생합니다. LinearLayout을 정의하는 데 정확히 동일한 코드가 있고 LinearLayout 내부에 TextView textview_systematics이 있습니다. I 은 TextView에 액세스 할 수 있지만 이 아니라 LinearLayout입니다. 여기서 뭐가 잘못 됐니?TextView에 액세스 할 수 있지만 같은 방법으로 선언 된 LinearLayout은 사용할 수 없습니다.

이 레이아웃은 단편이며 <fragment> -Tag에 포함되어 있습니다. 중요한 경우 일 수 있습니다.

XML 파일 (fragment_species_systematics.xml) :

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id    = "@+id/linlay_fragment_species_systematics" 
    android:layout_width = "match_parent" 
    android:layout_height = "wrap_content" 
    android:orientation  = "vertical" > 

    <TextView 
     android:id    = "@+id/textView_fragment_species_systematics" 
     android:layout_width = "match_parent" 
     android:layout_height = "wrap_content" /> 

</LinearLayout> 

자바 파일 :

public class Fragment_Species_Systematics extends Fragment { 

    LinearLayout linlay_container; 
    TextView  textview_systematics; 
    View   view; 

    public Fragment_Species_Systematics() {} 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.fragment_species_systematics, container, false); 

    return view; 
    } 



    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     linlay_container  = (LinearLayout) getActivity().findViewById(R.id.linlay_fragment_species_systematics); 
     textview_systematics = (TextView)  getActivity().findViewById(R.id.textView_fragment_species_systematics); 
    } 



    public void make_systematics(String json_systematics) { 
     if (textview_systematics.isAttachedToWindow()) {Log.i("DEBUG", "tv is attached");} else {Log.i("DEBUG", "tv is not attached");} 
     if (linlay_container.isAttachedToWindow())  {Log.i("DEBUG", "ll is attached");} else {Log.i("DEBUG", "ll is not attached");} 
    } 
} 

답변

0

불행하게도 어떤 이유로 을 찾지 못하고, 그것을 알아 낸 이유 나는 다음을 수행해야 : 분명히 하나는 "전역"LinearLayout에 액세스 할 수 없습니다. 나는 다른 LinearLayout을 포함해야하고 하나, 그때 그것은 작동합니다.

내 작업 XML 파일 :

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

    <LinearLayout 
     android:id    = "@+id/linlay_fragment_species_systematics" 
     android:layout_width  = "match_parent" 
     android:layout_height  = "wrap_content" 
     android:orientation  = "vertical" > 

     <TextView 
      android:id   = "@+id/textView_fragment_species_systematics" 
      android:layout_width = "match_parent" 
      android:layout_height = "wrap_content" /> 

    </LinearLayout> 
</LinearLayout> 
관련 문제