2014-09-01 36 views
1

我試圖從一個片段類訪問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而不更新文件。我刪除了該文件,但我忘記清理該項目。現在它工作正常。

+0

您是否嘗試過'links = view.findViewById()'而不是'sv.findviewById'? – Estel 2014-09-01 10:00:39

+0

在onCreateView中,我認爲你需要容器​​。嘗試這個。 view =(View)inflater.inflate(R.layout.fragment_home,container,false); – 2014-09-01 10:02:06

+0

我已經嘗試過(我假設你的R.layout.fragment_home,是我的R.layout.help_4) – 2014-09-01 10:13:24

回答

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 */ 
} 

此外,清潔一次構建,我很好奇,爲什麼你沒有得到的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; 
} 
+0

我首先使用了view並且獲得了nullpointerexception。我嘗試在XML中的第一個textview,我加載罰款。所以我想,也許問題是,我想要使用的textview是在scrollview中,爲此我嘗試了sv.findViewById(R.id.tvHelpText4); – 2014-09-01 10:10:24

+0

你的xml的名字是什麼?在返回視圖之前,將findViewById代碼移到onCreateView中。但是不需要使用scrollview來查找文本視圖 – user2450263 2014-09-01 10:11:53

+0

佈局的名稱:help_4 – 2014-09-01 10:14:24

2

與視圖

ScrollView sv = (ScrollView) view.findViewById(R.id.svText); 
links = (TextView) view.findViewById(R.id.tvHelpText4); 
links.setText("vblsarasdferadfkwersfadfwe"); 
2

變化links = (TextView) sv.findViewById(R.id.tvHelpText4)

更換SV到

links = (TextView) view.findViewById(R.id.tvHelpText4) 
1

您正在滾動型下創建的TextView但你havein查看即

所以repleace行:

links = (TextView) sv.findViewById(R.id.tvHelpText4); 

via

links = (TextView) view.findViewById(R.id.tvHelpText4);