将片段添加到底层 SurfaceView
Add fragment to underlying SurfaceView
如何向底层 SurfaceView 添加片段?我得到异常
java.lang.IllegalArgumentException: No view found for id 0x7f0b0050
我认为这是由于没有在 setContentView 中设置任何布局,例如
setContentView(R.layout.activity_main);
相反,我设置了对自定义表面视图的引用 ...
final GameSurface gs = new GameSurface(this);
setContentView(gs);
然后在 surfaceview 回调中 ontouch(...) - 如果用户点击 surface 应该加载片段
@Override
public boolean onTouchEvent(MotionEvent event) {
.....
FragmentManager fragmentManager =((Activity)context).getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExchangeFragment fragment = new ExchangeFragment();
fragmentTransaction.add(R.id.relLayout, fragment);
fragmentTransaction.commit();
提前致谢
编辑:
不再崩溃,但片段不会显示。为什么?
感谢@Kelevandos 和另一个堆栈线程
的回答
How to inflate one view with a layout
也就是说,我必须将 AttributSet 添加到自定义 surfaceViews 构造函数中。
少了什么?这是 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"
>
<se.brickgame.brickbreaker.GameSurface
android:id="@+id/surfaceId"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
编辑 2:
我也在代码中添加了这个
fragmentManager.executePendingTransactions();
System.out.println("FRAGMENT ISADDED = " + fragment.isAdded());
System.out.println("FRAGMENT VISIBLE = " + fragment.isVisible());
System.out.println("FRAGMENT HIDDEN = " + fragment.isHidden());
System.out.println("FRAGMENT ISINLAYOUT = " + fragment.isInLayout());
得到以下输出
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker ISystem.out: FRAGMENT ISADDED = true
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker I/System.out: FRAGMENT VISIBLE = true
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker I/System.out: FRAGMENT HIDDEN = false
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker I/System.out: FRAGMENT ISINLAYOUT = false
所以 .. 它似乎存在但不在布局中 - 如何解释?
问题是您的 GameSurface
中没有 ID 为 R.id.relLayout
的视图。必须为片段事务提供一个容器,您要将片段放入其中。
一种解决方案是为 Activity
创建布局,如下所示:
<RelativeLayout>
<GameSurface/>
<RelativeLayout/> //This is the Fragment container
</RelativeLayout>
将其设置为您的 Activity
的内容布局并像这样检索您的 GameSurface
:
final GameSurface gs = (GameSurface) findViewById(R.id.yourSurfaceId);
这实际上是在 Android 中处理布局和视图的方式,因此最好像这样重新设计您的解决方案。
如果出于某种原因您不能t/don不想这样做,您将不得不重新考虑用另一种不需要 Fragment
的方法替换 Fragment
部分。
如何向底层 SurfaceView 添加片段?我得到异常
java.lang.IllegalArgumentException: No view found for id 0x7f0b0050
我认为这是由于没有在 setContentView 中设置任何布局,例如
setContentView(R.layout.activity_main);
相反,我设置了对自定义表面视图的引用 ...
final GameSurface gs = new GameSurface(this);
setContentView(gs);
然后在 surfaceview 回调中 ontouch(...) - 如果用户点击 surface 应该加载片段
@Override
public boolean onTouchEvent(MotionEvent event) {
.....
FragmentManager fragmentManager =((Activity)context).getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExchangeFragment fragment = new ExchangeFragment();
fragmentTransaction.add(R.id.relLayout, fragment);
fragmentTransaction.commit();
提前致谢
编辑:
不再崩溃,但片段不会显示。为什么? 感谢@Kelevandos 和另一个堆栈线程
的回答How to inflate one view with a layout
也就是说,我必须将 AttributSet 添加到自定义 surfaceViews 构造函数中。
少了什么?这是 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"
>
<se.brickgame.brickbreaker.GameSurface
android:id="@+id/surfaceId"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<RelativeLayout
android:id="@+id/relLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</RelativeLayout>
编辑 2:
我也在代码中添加了这个
fragmentManager.executePendingTransactions();
System.out.println("FRAGMENT ISADDED = " + fragment.isAdded());
System.out.println("FRAGMENT VISIBLE = " + fragment.isVisible());
System.out.println("FRAGMENT HIDDEN = " + fragment.isHidden());
System.out.println("FRAGMENT ISINLAYOUT = " + fragment.isInLayout());
得到以下输出
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker ISystem.out: FRAGMENT ISADDED = true
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker I/System.out: FRAGMENT VISIBLE = true
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker I/System.out: FRAGMENT HIDDEN = false
11-25 16:09:07.957 12155-12155/se.brickgame.brickbreaker I/System.out: FRAGMENT ISINLAYOUT = false
所以 .. 它似乎存在但不在布局中 - 如何解释?
问题是您的 GameSurface
中没有 ID 为 R.id.relLayout
的视图。必须为片段事务提供一个容器,您要将片段放入其中。
一种解决方案是为 Activity
创建布局,如下所示:
<RelativeLayout>
<GameSurface/>
<RelativeLayout/> //This is the Fragment container
</RelativeLayout>
将其设置为您的 Activity
的内容布局并像这样检索您的 GameSurface
:
final GameSurface gs = (GameSurface) findViewById(R.id.yourSurfaceId);
这实际上是在 Android 中处理布局和视图的方式,因此最好像这样重新设计您的解决方案。
如果出于某种原因您不能t/don不想这样做,您将不得不重新考虑用另一种不需要 Fragment
的方法替换 Fragment
部分。