我正在尝试动态地将片段添加到我的XML布局中。布局:
<ScrollView
android:id="@+id/scrollView1"
android:layout_above="@+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rellayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<fragment
android:id="@+id/fragment1"
android:name="com.amaj.notetakertest.TopSectionFragment"
android:layout_width="match_parent"
android:layout_height="359dp" />
<EditText
android:id="@+id/et_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/fragment1"/>
</RelativeLayout>
</ScrollView>
我正在尝试动态地将另一个片段(称为fragment2)添加到rellayout1
内的ScrollView
,如上所示。但是当动态添加fragment2时,应用程序崩溃了。
如果我从布局中删除ScrollView然后将片段动态添加到rellayout1
,则不会发生崩溃。如果我将片段动态添加到ScrollView之外的其他布局,也不会发生这种情况。我的问题是:什么是导致功能障碍的ScrollView?
动态添加片段的代码如下:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rellayout1);
TopSectionFragment f = new TopSectionFragment();
FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fTrans = fm.beginTransaction();
fTrans.add(rl.getId(), f);
fTrans.addToBackStack(null);
fTrans.commit();
上述Java代码在发生单击事件时运行。
为了澄清一下,TopSectionFragment类确实扩展了android.support.v4.app.Fragment
,并且该类中的所有内容都没有问题。我可以将片段完美地添加到其他布局中。只是每当我尝试在ScrollView的内部布局中添加它时,它就会崩溃。与其他测试应用程序一起独立尝试。我认为这也不是缺少LayoutParams,因为即使我添加了LayoutParams,问题仍然存在。
我错过了什么?
编辑:这是日志:
05-01 21:32:29.010 27452-27469/com.amaj.notetakertest E/EGL_emulation: tid
27469: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
05-01 21:32:33.207 27452-27452/com.amaj.notetakertest E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.amaj.notetakertest, PID: 27452
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:877)
at android.graphics.Bitmap.createBitmap(Bitmap.java:856)
at android.graphics.Bitmap.createBitmap(Bitmap.java:823)
at com.amaj.notetakertest.CanvasView.onSizeChanged(CanvasView.java:46)
at android.view.View.sizeChange(View.java:17641)
at android.view.View.setFrame(View.java:17603)
at android.view.View.layout(View.java:17520)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.widget.ScrollView.onLayout(ScrollView.java:1535)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17523)
at android.view.ViewGroup.layout(ViewGroup.java:5612)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
基于日志,在CanvasView类中调用onSizedChanged()方法时似乎存在问题,该类是片段布局中的画布。
CanvasView类中的方法如下:
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
将CanvasView添加到片段中如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#FFFFFF">
<com.amaj.notetakertest.CanvasView
android:id="@+id/signature_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
答案 0 :(得分:0)
这个想法是
父布局↓
<RelativeLayout>
然后添加工具栏↓
<include layout="@layout/toolbar_layout"/>
然后ScrollView↓
<ScrollView/>
最后您需要的页面布局↓
<RelativeLayout/>