片段:java.lang.IllegalArgumentException:找不到ID为0x7f0c006f的视图

时间:2016-09-05 15:39:17

标签: java android android-fragments

我试图从一个片段移动到第二个片段 它显示以下错误:

$workorderid = null;

foreach ($array_data['response']['operation']['Details'] as $detail) {
    if (isset($detail['workorderid'])) {
        $workorderid = $detail['workorderid'];
        break;
    }
}

if (null !== $workorderid) {
    var_dump($workorderid);
}

这是我的第一个片段:

java.lang.IllegalArgumentException: No view found for id 0x7f0c006f (com.chinatown.wangjian.chinatownbang:id/viewpager) for fragment TwoFragment{10598077 #1 id=0x7f0c006f android:switcher:2131492975:1}

我的第二个片段是这样的:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_scrolling, container, false);
        // Inflate the layout for this fragment

        getActivity().setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });



        return v;

我是否正确编码?

2 个答案:

答案 0 :(得分:1)

在您的代码中,您似乎正在应用2个布局。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_scrolling, container, false);
    //why setContentView?
    //getActivity().setContentView(R.layout.activity_scrolling);
    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });



    return v;

答案 1 :(得分:1)

首先,根据你的代码,你不会从第一个片段中调用第二个片段。

将此代码添加到您的第一个片段中

FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction();
//CREATE OBJECT OF FRAGMENT WHERE YOU WANT TO GO
fragment_2 frag2=new fragment_2();
fragmentTransaction.add(R.id.fragment_container,frag2);
fragmentTransaction.commit();

这就是你如何从一个片段中启动下一个片段,你还在你的片段中调用两个布局,如果你使用上面的代码加上片段只使用一个布局

会更好