我正在尝试找到一种方法来动态地将片段添加到活动布局,同时唯一地设置该片段的TextView。假设我在下面有片段类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时(使用片段中的setter方法),片段正确添加。但是,如果我尝试从Fragment中为任何TextView设置setText,则应用程序崩溃。我在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
类中,当我删除崩溃应用程序的两行代码时,片段添加正常但没有自定义TextView。我试图找出一种方法来独特地设置TextViews(通过片段中的方法)而不会崩溃应用程序。
任何回复都表示赞赏。谢谢。
编辑:找到了我的问题的解决方案,在下面回答。
答案 0 :(得分:1)
我终于通过查看this帖子了解了这一点。我必须先将这些方法添加到我的ExampleFragment
类:
public static ExampleFragment newInstance(String name, String location) {
ExampleFragment myFragment = new ExampleFragment();
Bundle args = new Bundle();
args.putString("name", name);
args.putString("location", location);
myFragment.setArguments(args);
return myFragment;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String name = getArguments().getString("name");
String location = getArguments().getString("location");
Name.setText(name);
Location.setText(location);
然后在ExampleActivity中,而不是将片段实例化为:
ExampleFragment fragment = new ExampleFragment();
我将其实例化为:
ExampleFragment fragment = ExampleFragment.newInstance(o.getName(), o.getLocation());
除了删除OP中最初导致它崩溃的两行代码。 (fragment.setName()
和fragment.setLocation()
语句。
问题是我在初始化之前尝试在TextView上设置文本(在调用onCreateView之前)。这种实例化片段的方式可确保在创建片段视图之前不会设置TextView。 This帖子也是一个很好的参考,以及Android网站上的documentation。
答案 1 :(得分:0)
试试此代码
片段
public class ExampleFragment extends Fragment {
ArrayList<ExampleObject> list = new ArrayList<ExampleObject>();
TextView Name, Location;
public ExampleFragment(ArrayList<ExampleObject> arrayList){
list=arrayList;
}
@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);
for(ExampleObject o: list)
{
setName(o.getName());
setLocation(o.getLocation());
}
return view;
}
public void setName(String n)
{
Name.setText(n);
}
public void setLocation(String loc)
{
Location.setText(loc);
}
}
活动
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"));
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment(list);
fragmentTransaction.add(R.id.linear_layout_scroll, fragment);
fragmentTransaction.commit();
}
}