我的应用有一个带有两个标签的标签式活动。我希望另一个视图abcd.xml在选项卡的片段中膨胀。我试过这样做但是没有用。
这是片段扩展的代码。 IncomeFrag.java
public class IncomeFrag extends Fragment{
private static final String TAG = "Income Frag";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.income_frag, container, false);
populate();
return view;
}
private void populate() {
SQLiteDatabase mydatabase = getActivity().openOrCreateDatabase("tracker",MODE_PRIVATE,null);
Intent intent = getActivity().getIntent();
int user_id = Integer.parseInt(intent.getStringExtra("user_id"));
LinearLayout linearLayout = (LinearLayout) getActivity().findViewById(R.id.inc_frag_tab);
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
Cursor rs = mydatabase.rawQuery("select * from transactions where user_id = " + user_id + ";", null);
if (rs.moveToLast()) {
do {
View pastItem = vi.inflate(R.layout.abcd, null);
int amount = rs.getInt(2);
String cat = rs.getString(3);
String description = rs.getString(4);
String type = rs.getString(5);
String date = rs.getString(6);
if (type.equalsIgnoreCase("income")) {
TextView amt_tv = (TextView) pastItem.findViewById(R.id.amount_inflator);
amt_tv.setText(Integer.toString(amount));
amt_tv.setTextColor(getResources().getColor(R.color.green));
TextView cat_tv = (TextView) pastItem.findViewById(R.id.category_inflator);
cat_tv.setText(cat);
TextView desc_tv = (TextView) pastItem.findViewById(R.id.description_inflator);
desc_tv.setText(description);
TextView date_tv = (TextView) pastItem.findViewById(R.id.date_inflator);
date_tv.setText(date);
linearLayout.addView(pastItem);
}
} while (rs.moveToPrevious());
}
rs.close();
}
catch (Exception e) {
// Nothing
}
}
}
这是我要扩充abcd.xml的视图的xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="7sp">
<TextView
android:id="@+id/amount_inflator"
android:text="@string/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#FF0000"
android:paddingBottom="3sp"/>
<TextView
android:id="@+id/category_inflator"
android:text="abcd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingBottom="2sp"/>
<TextView
android:id="@+id/description_inflator"
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/date_inflator"
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="15sp" />
</LinearLayout>
我希望它在这里充气
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/inc_frag_tab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
</LinearLayout>
答案 0 :(得分:3)
使用片段时,无法从 onCreateView()方法访问LinearLayout或TextView等视图。尝试将 populate()方法放在 onViewCreated()中,而不是这样,看看它是否有效。
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.income_frag, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
populate();