java - 未找到 fragment MyFragment id 0x 的 View

标签 java android android-layout android-fragments crash

我在 FragmentManager 中调用新 fragment 时遇到问题。 当我尝试在主 Activity 中调用 fragment 时,我收到此日志,但我不知道为什么:

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>

这肯定有效,我希望它能帮助您找到解决方案

关于java - 未找到 fragment MyFragment id 0x 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24129619/

相关文章:

java - Jersey WADL 生成器支持自定义注释

android - 如何将数据从游标添加到viewpager

java - EditText AdjustResize 强制内容不可见

Android:如何在 DateRangePicker 中设置一周的第一天?

android - 如何在 Android Studio 中检查元素

java - 在 java 中使用 IP 地址查找位置的示例代码?

java - SWT 空指针异常

java - 如果数组有效,如何创建传递的数组的深拷贝?

android - 如何在 StaggeredGridView 底部添加 "load more" View

android - 禁用/过滤布局的任何触摸事件