我试图从一个片段类访问scrollview里面的textview,我试着从其他相关问题中找到的所有建议,但我总是在settext中得到nullpointerexception。scrollview内textview的空指针异常
这里是我的片段类:
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 感谢大家的帮助。问题出在资源上。我在layout-hdpi文件夹中有上述xml的另一个副本。在某些情况下,我更改了id而不更新文件。我删除了该文件,但我忘记清理该项目。现在它工作正常。
您是否尝试过'links = view.findViewById()'而不是'sv.findviewById'? – Estel 2014-09-01 10:00:39
在onCreateView中,我认为你需要容器。尝试这个。 view =(View)inflater.inflate(R.layout.fragment_home,container,false); – 2014-09-01 10:02:06
我已经尝试过(我假设你的R.layout.fragment_home,是我的R.layout.help_4) – 2014-09-01 10:13:24