2014-02-27 45 views
0

我有玩游戏的游戏表面。现在我需要在图像上使用一些按钮。但我需要使用XML布局文件来在游戏视图中添加按钮。我使用XML的布局,但我越来越使用XML布局在游戏SurfaceView上添加按钮

Error Inflating class com.name.company.surfaceview.GameView 

代码:

public class MainActivity extends Activity { 
    GameView GameView; 
    Button btnStart; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //for no title 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     //setContentView(new GameView(this)); 

     setContentView(R.layout.activity_main); 
     GameView = (GameView)findViewById(R.id.gameView); 
     btnStart = (Button)findViewById(R.id.btnStart); 

    } 

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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 



    <view class="com.name.company.surfaceview$GameView" 
     android:id="@+id/gameView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

    <LinearLayout android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom"> 

     <Button 
      android:id="@+id/btnStart" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button" /> 


    </LinearLayout> 

</RelativeLayout> 

回答

0

我觉得你Java代码似乎并没有有问题。 但是,可能与您的XML文件有关。更换你XML文件与这一个:

<LinearLayout 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" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 



<view class="com.name.company.surfaceview$GameView" 
    android:id="@+id/gameView" 
android:layout_width="match_parent" 
android:layout_height="match_parent"/> 


    <Button 
     android:id="@+id/btnStart" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button"/> 

</LinearLayout> 

如果这个代码不工作,那么问题出在classcom.name.company.surfaceview$GameView。再次尝试相同的代码,但将button项目放在class项目上方。我希望它有帮助

+0

它不工作。如果我从xml中删除视图并从java中删除此行Game =(GameView)findViewById(R.id.gameView);该按钮显示..但是,当我包括GameView它不工作。 – user3351727

+0

请确保高度和宽度都是'match_parent'而不是'fill_parent'是 –

+0

yes.It是匹配的父级..我的包名称是com.name.company .. – user3351727