2016-11-24 7 views
0

기본 SurfaceView에 조각을 어떻게 추가합니까? 나는 예외를 얻을기본 SurfaceView에 조각 추가

java.lang.IllegalArgumentException: No view found for id 0x7f0b0050 

나는 ... 이것이 내가 정의 서피스 뷰 SurfaceView에 대한 참조를 설정 같은

대신
setContentView(R.layout.activity_main); 

된 setContentView의 모든 레이아웃을 설정하지 않는 때문에 생각

final GameSurface gs = new GameSurface(this); 
setContentView(gs); 

다음 표면 뷰 콜백 ontouch (...) - 사용자가 표면을 클릭하면 조각이로드됩니다.

사전에 363,210
@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

의 대답은, 나는 사용자 정의 surfaceViews 생성자에 AttributSet을 추가 HADE.

무엇이 누락 되었습니까? 다음은 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 

그래서 .. 거기에있는 것 같다. 레이아웃에 없다 - 어떻게 해석 할까?

답변

1

GameSurface에는 내부에 R.id.relLayout이라는보기가 없습니다. 단편 트랜잭션에는 단편을 넣으려는 컨테이너가 있어야합니다. 당신의 Activity의 콘텐츠 레이아웃으로

<RelativeLayout> 
    <GameSurface/> 
    <RelativeLayout/> //This is the Fragment container 
</RelativeLayout> 

설정을하고 GameSurface 같은 검색 :

이를

하나 개의 솔루션이처럼 Activity에 대한 레이아웃을 생성하는 것입니다 실제로 Android에서 레이아웃과 뷰를 처리해야하는 방식이므로 솔루션을 이와 같이 재 설계하면 가장 좋습니다.

어떤 이유로 든 원하지 않을 경우 Fragment 부분을 다른 방법으로 교체해야합니다 (Fragment은 필요하지 않음).

+0

감사하지만 조각이 나타나지 않습니다. 나는 지금 그것을 이해할 수 없다, 편집을 참조하십시오. – java

+0

이 답변으로 많은 도움이되었습니다. – java