繁体   English   中英

找不到片段MyFragment的ID 0x的视图

[英]No view found for id 0x for fragment MyFragment

我在FragmentManager中调用新片段时遇到问题。 当我尝试在主要活动中调用该片段时,收到此日志,但不知道为什么:

Process: com.robertot.timereport, PID: 31255
    java.lang.IllegalArgumentException: No view found for id 0x7f07000a (com.robertot.timereport:id/content_frame) for fragment MyFragment{64c07710 #0 id=0x7f07000a}

这是我的MainActivity类:

public class MainActivity extends FragmentActivity
{
     @Override
     protected void onCreate(Bundle savedInstanceState) 
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //...

     }

     private void LoadFragment(int position)
     {
        Fragment frgm = null;
        FragmentManager fm = getSupportFragmentManager();

        switch(position)
        {
            case 1:
                frgm = new MyFragment();
                fragmentTag = "newjob";
                break;
        }

        fm.beginTransaction()
               .replace(R.id.content_frame, frgm, fragmentTag)   // CRASH HERE
                .commit();
    }
}

这是我的activity_main 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"
    android:id="@+id/content_frame">
</FrameLayout>

这是MyFragment类:

public class MyFragment extends Fragment
{
    private CardListView listView;
    private InterfaceProgressBar listenerProgrBar;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        listenerProgrBar = (InterfaceProgressBar) activity;
    }

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        listView = (CardListView) view.findViewById(R.id.cardlist);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        //...do works
    }
}

我究竟做错了什么?

感谢您的支持!

这是创建项目时的android示例:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

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

}

activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asdsd.MainActivity"
    tools:ignore="MergeRootFrame" />

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.asdsd.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

这应该可以正常工作,希望它可以帮助您找到解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM