2014-07-25 3 views
-3

Fragment 내에서 프로그래밍 방식으로 TextView의 텍스트를 설정하려고합니다. 클래스의 코드는 다음과 같습니다안드로이드 TextView setText 결과가 빈 텍스트

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" > 

    <TextView 
     android:id="@+id/text_pdf_filename_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/text_pdf_filename" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/text_pdf_file_name" 
     android:layout_marginLeft="15dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

이상한 일이 : 또 다른에, 작동 한 조각 내에서 수행하지 (부모 활동을 다음과 같이

public abstract class AbstractFragment extends Fragment 
{ 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) 
    { 
     this.setFileName(); 
    } 

    protected abstract void setFileName(); 
} 

public class ImplementingFragment extends AbstractFragment 
{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 
     View fragment = inflater.inflate(
       R.layout.fragment_layout, container, false); 

     return fragment; 
    } 

    @Override 
    protected void setFileName() 
    { 
     String fileName = "Test.txt"; 

     TextView textView = ((TextView) getActivity().findViewById(
       R.id.text_file_name)); 
     textView.setText(fileName); 
    } 
} 

레이아웃입니다). 또 다른 사실은 텍스트를 설정 한 후 textView.getText()를 통해 텍스트를 가져온다는 것입니다. 나중에 코드에서 텍스트를 가져 오려고하면 빈 문자열이 생깁니다.

또한 코드를 디버깅하면 텍스트가 사라지기 전에 밀리 초 단위로 표시됩니다.

누구나 해결 방법은 있습니까?

+2

'fileName' 무엇인가? settign 전에 fileName 값을 textview 및 chekc에 기록하십시오. – Raghunandan

+0

@Raghunandan : fileName 값은 문자열입니다. 그것은 비어 있지 않습니다 (나는 그것을 확인합니다). –

+0

fileName이 비어 있지 않으면 textView가 null이어야 NullPointerExcpetion이됩니다. 참고 : setText는 null을 취할 수 있습니다. – Raghunandan

답변

-1

내부 조각 사용 :

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

    TextView textView = ((TextView) rootView.findViewById(
      R.id.text_file_name)); 
    textView.setText(fileName); 

    return rootView; 
} 
+0

조각이 올바른 방법으로 부풀려져 있습니다. 내 방법은 TextView 통찰력을 얻을. 같은 부모와 다른 뷰가 작동합니다. –

+0

@der_chirurg 더 많은 코드를 게시해야합니다. 텍스트 뷰가 속한 레이아웃과 관련 조각 코드. 그것의 어려운 지금 당장 hapenning 무엇 – Raghunandan

+0

@ der_chirurg 당신이 단편에서 활동으로 의사 소통 할 수있는 방법과 콜백으로 인터페이스를 사용하여 활동 자체의 텍스트를 설정할 수 있습니다 – Raghunandan