我试图从片段类访问一个滚动视图内的textview,我尝试了从其他相关问题中找到的所有建议,但我总是在settext上得到nullpointerexception。
这是我的片段类:
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>
更新 感谢大家的帮助。问题出在资源上。我在layout-hdpi文件夹上有上述xml的另一个副本。在某些时候,我更改了id而不更新文件。我删除了该文件,但我忘了清理该项目。现在它工作正常。
答案 0 :(得分:2)
用视图替换sv
ScrollView sv = (ScrollView) view.findViewById(R.id.svText);
links = (TextView) view.findViewById(R.id.tvHelpText4);
links.setText("vblsarasdferadfkwersfadfwe");
答案 1 :(得分:2)
更改links = (TextView) sv.findViewById(R.id.tvHelpText4)
到
links = (TextView) view.findViewById(R.id.tvHelpText4)
答案 2 :(得分:2)
您的textview
是包含scrollview
的同一xml的一部分。
xml中的层次结构不会影响findViewById(s)
ScrollView sv = (ScrollView) view.findViewById(R.id.svText);
links = (TextView) sv.findViewById(R.id.tvHelpText4);
应该是
ScrollView sv = (ScrollView) view.findViewById(R.id.svText);
links = (TextView) view.findViewById(R.id.tvHelpText4);
您会发现textview是同一视图的一部分。
编辑1:
@Override
public void onViewCreated (View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ScrollView sv = (ScrollView) view.findViewById(R.id.svText);
links = (TextView) view.findViewById(R.id.tvHelpText4);
links.setText("vblsarasdferadfkwersfadfwe");
//links.setMovementMethod(LinkMovementMethod.getInstance());
/* Other code here */
}
另外,清理构建一次,我很好奇为什么你没有获得用于scrollview的NPE以及仅用于那种情况下的textview。
为此,看看如果findViewById转移到onCreateView会发生什么:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
view = inflater.inflate(R.layout.help_4, null);
ScrollView sv = (ScrollView) view.findViewById(R.id.svText);
links = (TextView) view.findViewById(R.id.tvHelpText4);
links.setText("vblsarasdferadfkwersfadfwe");
return view;
}
答案 3 :(得分:1)
您正在ScrollView下创建Textview,但您已查看,即
如此充满理由:
links = (TextView) sv.findViewById(R.id.tvHelpText4);
通过
links = (TextView) view.findViewById(R.id.tvHelpText4);