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()를 통해 텍스트를 가져온다는 것입니다. 나중에 코드에서 텍스트를 가져 오려고하면 빈 문자열이 생깁니다.
또한 코드를 디버깅하면 텍스트가 사라지기 전에 밀리 초 단위로 표시됩니다.
누구나 해결 방법은 있습니까?
'fileName' 무엇인가? settign 전에 fileName 값을 textview 및 chekc에 기록하십시오. – Raghunandan
@Raghunandan : fileName 값은 문자열입니다. 그것은 비어 있지 않습니다 (나는 그것을 확인합니다). –
fileName이 비어 있지 않으면 textView가 null이어야 NullPointerExcpetion이됩니다. 참고 : setText는 null을 취할 수 있습니다. – Raghunandan