Я пытаюсь получить доступ к текстовому представлению, которое находится внутри scrollview из класса фрагмента, и я пробовал все предложения, которые я нашел из других относительных вопросов, но я всегда получаю nullpointerexception в settext.Исключительное исключение указателя на textview внутри scrollview
Вот мой фрагмент класс:
View view;
TextView links;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
view = inflater.inflate(R.layout.help_4, null);
return view;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState)
{
ScrollView sv = (ScrollView) view.findViewById(R.id.svText);
links = (TextView) sv.findViewById(R.id.tvHelpText4);
links.setText("vblsarasdferadfkwersfadfwe");
//links.setMovementMethod(LinkMovementMethod.getInstance());
/* Other code here */
}
и вот XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/help_settings"
android:id="@+id/textView"
android:background="#0570A9"
android:clickable="true"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:paddingEnd="5dp"
android:paddingStart="5dp"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:id="@+id/svText">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/help_text4"
android:id="@+id/tvHelpText4"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:autoLink="all"/>
</ScrollView>
</LinearLayout>
UPDATE Спасибо всем за вашу помощь. Проблема заключалась в том, чтобы возобновить работу. У меня была другая копия вышеупомянутого xml в папке layout-hdpi. В какой-то момент я изменил идентификатор, не обновляя файл. Я удалил файл, но я забыл очистить проект. Теперь он работает нормально.
Вы пробовали 'links = view.findViewById()' вместо 'sv.findviewById'? – Estel
В onCreateView, я думаю, вам нужен контейнер. попробуй это. view = (View) inflater.inflate (R.layout.fragment_home, контейнер, false); –
Я уже пробовал (предположим, что ваш R.layout.fragment_home, мой R.layout.help_4) –