码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment(10)FragmentTransaction.add(id,Fragment)报错: No view found for id 0x****** for fragment

时间:2015-06-27 01:08:54      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

1.错误信息:

06-26 22:54:28.509: E/AndroidRuntime(20363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exe.custommenu/com.exe.custommenu.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0xaabbcc for fragment LeftFrgmt{42956258 #0 id=0xaabbcc}

2.错误代码

public class MainActivity extends Activity {
    private MainUI mainui;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainui = new MainUI(this);
        setContentView(mainui);
        if (savedInstanceState == null) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(MainUI.LEFT_ID,new LeftFrgmt());//报错
            ft.add(MainUI.MIDEELE_ID, new MiddleFrgmt());
            ft.add(MainUI.RIGHT_ID, new Fragment());
            ft.commit();
        }
    }
}



3.原因:

add时使用layout id必需是setContentView时所用view对应的layout里的子layout,

下文来自 stackoverflow

I was having this problem too, until I realized that I had specified the wrong layout in setContentView() of the onCreate() method of the FragmentActivity.

The id passed into FragmentTransaction.add(), in your case R.id.feedContentContainer, must be a child of the layout specified in setContentView().

You didn‘t show us your onCreate() method, so perhaps this is the same problem.

 

4.解决方法:在setContentView的layout中指定传递给add的子layout及id

public class MainUI extends RelativeLayout {
    private Context context;
    private FrameLayout leftMenu;
    private FrameLayout middleMenu;
    private FrameLayout rightMenu;
    private FrameLayout middleMask;
    private Scroller mScroller;
    public static final int LEFT_ID = 0xaabbcc;
    public static final int MIDEELE_ID = 0xaaccbb;
    public static final int RIGHT_ID = 0xccbbaa;

    public MainUI(Context context) {
        super(context);
        initView(context);
    }

    public MainUI(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        mScroller = new Scroller(context, new DecelerateInterpolator());
        leftMenu = new FrameLayout(context);
        middleMenu = new FrameLayout(context);
        rightMenu = new FrameLayout(context);
        middleMask = new FrameLayout(context);
        leftMenu.setBackgroundColor(Color.RED);
        middleMenu.setBackgroundColor(Color.GREEN);
        rightMenu.setBackgroundColor(Color.RED);
        middleMask.setBackgroundColor(0x88000000);
        leftMenu.setId(LEFT_ID);
        middleMenu.setId(MIDEELE_ID);
        rightMenu.setId(RIGHT_ID);
        addView(leftMenu);
        addView(middleMenu);
        addView(rightMenu);
        addView(middleMask);
        middleMask.setAlpha(0);
    }
        //...
}

 

Fragment(10)FragmentTransaction.add(id,Fragment)报错: No view found for id 0x****** for fragment

标签:

原文地址:http://www.cnblogs.com/cocl/p/4603459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!