【发布时间】:2017-10-31 13:24:04
【问题描述】:
我正在尝试找到一种方法来动态地将片段添加到活动布局中,同时唯一地设置该片段的 TextViews。假设我在下面有片段类ExampleFragment:
package com.project.testapplication;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.project.testapplication.R;
public class ExampleFragment extends Fragment {
TextView Name, Location;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
Name=(TextView)view.findViewById(R.id.name);
Location=(TextView)view.findViewById(R.id.location);
return view;
}
public void setName(String n)
{
Name.setText(n);
}
public void setLocation(String loc)
{
Location.setText(loc);
}
}
其中使用fragment_layout.xml的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:text="location" />
</RelativeLayout>
现在在另一个活动中,我想将用户指定数量的这些片段添加到布局中,并为每个片段添加用户指定的名称和位置(设置为 TextView)。我正在使用 FragmentManager 将片段添加到我的活动中。当我不尝试编辑片段中的任何 TextView(使用片段中的设置器方法)时,片段会正确添加。但是,如果我尝试为 Fragment 中的任何 TextViews 设置文本,应用程序就会崩溃。我在ExampleActivity 类中的代码(包含导致程序崩溃的行):
package com.project.testapplication;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.project.testapplication.ExampleFragment;
import com.project.testapplication.ExampleObject;
import com.project.testapplication.R;
import java.util.ArrayList;
public class ExampleActivity extends AppCompatActivity {
ArrayList<ExampleObject> list = new ArrayList<ExampleObject>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
//Populate "list" ArrayList with "ExampleObject" objects
list.add(new ExampleObject("name1", "location1"));
list.add(new ExampleObject("name2", "location2"));
list.add(new ExampleObject("name3", "location3"));
for(ExampleObject o: list)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragment.setName(o.getName()); /***line causes crash***/
fragment.setLocation(o.getLocation()); /***line causes crash (if not for line above)***/
fragmentTransaction.add(R.id.linear_layout_scroll, fragment);
fragmentTransaction.commit();
}
}
}
ExampleObject 定义如下:
package com.project.testapplication;
public class ExampleObject {
String name, location;
public ExampleObject(String name, String location)
{
this.name=name;
this.location=location;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setLocation(String location)
{
this.location=location;
}
public String getLocation()
{
return location;
}
}
在我的activity_example.xml 布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lin_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.project.testapplication.ExampleActivity">
<ScrollView
android:id="@+id/scroll_v"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear_layout_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
在ExampleActivity 类中,当我删除导致应用程序崩溃的两行代码时,片段添加得很好,但没有自定义的 TextViews。我正在尝试找出一种方法来唯一地设置 TextViews(通过片段中的方法)而不会使应用程序崩溃。
感谢任何回复。谢谢。
编辑:找到了我的问题的解决方案,在下面回答。
【问题讨论】:
标签: java android android-layout android-fragments dynamic