![](/img/trans.png)
[英]java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView
[英]java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ScrollView
我有一段時間遇到此錯誤,在這里搜索解決方案,但沒有任何效果。 我試圖以編程方式在片段中添加滾動視圖,該片段具有一個linearlayout作為根元素。
我的片段布局xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#481327"
tools:context="com.gov.dmrd.disastermanagement.TestFragment3">
</LinearLayout>
片段oncreateview
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
LinearLayout innerLayout = (LinearLayout) inflater.inflate(R.layout.fragment_test_fragment3, container, false);
innerLayout.setOrientation(LinearLayout.VERTICAL);
innerLayout.setScrollBarStyle(LinearLayout.SCROLLBARS_INSIDE_OVERLAY);
ScrollView sv = (ScrollView) inflater.inflate(R.layout.fragment_test_fragment3, container, false);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
name = getResources().getStringArray(R.array.personnel_name);
rank = getResources().getStringArray(R.array.personnel_rank);
for (int i = 0; i < name.length; i++){
TextView tv1 = new TextView(container.getContext());
tv1.setText(name[i]);
innerLayout.addView(tv1, params);
TextView tv2 = new TextView(container.getContext());
tv2.setText(rank[i]);
innerLayout.addView(tv2, params);
}
sv.addView(innerLayout);
return sv;
}
java.lang.ClassCastException:android.widget.LinearLayout無法轉換為android.widget.ScrollView
因為試圖將LinearLayout
為ScrollView
我正在嘗試以編程方式在片段中添加滾動視圖,該片段具有linearlayout作為根元素
動態創建ScrollView
並將其添加到innerLayout
:
View view=(View)inflater.inflate(R.layout.fragment_test_fragment3,
container, false);
ScrollView sv =new ScrollView(getActivity()); //<< create ScrollView object
LinearLayout innerLayout=new LinearLayout(getActivity());//<<create LinearLayout
//...your code here
sv.addView(innerLayout);
view.addView(sv);
return view;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.