从活动崩溃中更新片段ViewPager中的视图

时间:2019-05-08 02:13:08

标签: android

在我的活动中,有一个ViewPager和一个按钮更新:

public class Main7Activity extends AppCompatActivity {
    ViewPager vp;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);
        btn = findViewById(R.id.btn_update);
        vp = findViewById(R.id.vp);
        final AdapterEx adapterEx = new AdapterEx(getSupportFragmentManager());
        vp.setAdapter(adapterEx);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vp.getCurrentItem() == 1 && adapterEx != null) {
                    AdapterEx fa = (AdapterEx) vp.getAdapter();
                    FragmentTow mstPeopleFragment = (FragmentTow) fa.getItem(vp.getCurrentItem());
                    mstPeopleFragment.update();
                }
            }
        });
    }

    private class AdapterEx extends FragmentStatePagerAdapter {
        public AdapterEx(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return new FragmentOne();
                case 1:
                    return new FragmentTow();
                default:
                    return new FragmentTow();
            }

        }
        @Override
        public int getCount() {
            return 2;
        }
    }
}

FragmentTwo中,我有两个文本:组和团队,我想在页面显示FragmentTwo并单击活动中的按钮更新时显示文本teams,文本groups将在FragmentTwo中禁用。 这是FragmentTwo中的代码:

public class FragmentTow extends Fragment {
    TextView textTeam;
    TextView textGroup;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_two, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        textTeam = view.findViewById(R.id.tv_team);
        textGroup = view.findViewById(R.id.tv_group);
    }
    public void  update(){
        textTeam.setVisibility(View.GONE);
        textGroup.setVisibility(View.VISIBLE);
    }
}

这是FragmentTwo fragment_two.xml

中的xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_team"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="groups"
        android:textSize="50sp" />

    <TextView
        android:id="@+id/tv_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Teams"
        android:textSize="50sp"
        android:visibility="gone" />
</FrameLayout>

在“活动”中单击“更新按钮”时,我将在update()中调用FragmentTwo函数,但日志崩溃:Process: com.example.test2, PID: 32755 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference at com.example.test2.FragmentTow.update(FragmentTow.java:29) at com.example.test2.Main7Activity$1.onClick(Main7Activity.java:30) 我尝试调试,看看从活动中获得片段时,它将在堆栈的另一个本地中返回FragmentTwo,因此调用FragmentTwoNullPointerException中的文本将update()。 在“活动”中单击按钮时如何更新FragmentTwo中的文本?

3 个答案:

答案 0 :(得分:0)

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_groups, container, false);
}

R.layout.fragment_groups的布局FragmentTwo吗?否则,textTeamtextGroup将为空

编辑

尝试一下:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    textTeam = getView().findViewById(R.id.tv_team);
    textGroup = getView().findViewById(R.id.tv_group);
}

答案 1 :(得分:0)

您可以简单地尝试使用function drawChart(data,data1) { var svgWidth = 600, svgHeight = 400; var margin = { top: 20, right: 20, bottom: 30, left: 50 }; var width = svgWidth - margin.left - margin.right; var height = svgHeight - margin.top - margin.bottom; var svg = d3.select('svg') .attr("width", svgWidth) .attr("height", svgHeight); var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")" ); var x = d3.scaleLinear().rangeRound([0, width]); var y = d3.scaleLinear().rangeRound([height, 0]); var line = d3.line() .x(function(d) { return x(d.x)}) .y(function(d) { return y(d.y)}) x.domain(d3.extent(data1, function(d) { return d.x })); y.domain(d3.extent(data1, function(d) { return d.y })); g.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)) .append("text") .attr("fill", "#000") .text("Foreign Export ($)") .attr("dy", "0.71em") .attr("y", 20) .attr("x",500) .select(".domain") .remove() g.append("g") .call(d3.axisLeft(y)) .append("text") .attr("fill", "#000") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "0.90em") .attr("text-anchor", "center") .text("Price ($)") g.append('svg:circle') //problem .datum(data) .attr("cx", function(d) { return (d.x); }) .attr("cy", function(d) { return (d.y); }) .attr("r", 8); g.append("path") .datum(data1) .attr("fill", "none") .attr("stroke", "steelblue") .attr("stroke-linejoin", "round") .attr("stroke-linecap", "round") .attr("stroke-width", 1.5) .attr("d", line); } drawChart(data,data1); 从Main7Activity中的片段中获取TextView,如下所示:-

mstPeopleFragment.getView()

然后相应地更新它们,因此添加它:-

textTeam = mstPeopleFragment.getView().findViewById(R.id.tv_team);
textGroup = mstPeopleFragment.getView().findViewById(R.id.tv_group);

但是请记住将它们保留在按钮侦听器中,否则将不起作用。

答案 2 :(得分:0)

我找到了解决方案。使用代码更新新的AdapterEx

AdapterEx extends FragmentPagerAdapter {

    ArrayList<Fragment> fragments = new ArrayList<>();
   
    public void addFragments(Fragment fragment) {
        this.fragments.add(fragment);
    }

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

当点击更新时,我打电话给 FragmentTow fragmentTow = (FragmentTow) viewPagerAdapter.getItem(1); fragmentTow.update(); 感谢您的宝贵时间! 非常感谢!