0

这是我的问题:

在我的应用程序中,我使用了 2 种布局;一个是主菜单,另一个是实际的游戏内布局。当我开始我的游戏时,我希望删除主菜单布局。我想过这样做使用

myView.setVisibility(View.GONE);

然后使用相同的方法制作其他布局。

问题是,当我尝试让布局消失时,我的应用程序会关闭,并且会出现告诉我的消息框。

编辑:以下代码已更改

public class DemoReelActivity extends Activity implements OnClickListener{

private GLSurfaceView mGLView;
private View mainMenu;                          

Button newGame = null;

Button shoot = null;
SeekBar power = null;

TextView force = null;

byte forcePct = 0;

ball[] ballSet; 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

mainMenu =  findViewById(R.layout.main_menu);                                                                                          setContentView(R.layout.main_menu);                      

    newGame = (Button) findViewById(R.id.button1);
    newGame.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    startGame();

}

public void startGame()
{
    //Remove the main_menu view
    mainMenu.setVisibility(View.GONE);
    mGLView.setVisibility(View.VISIBLE);

    // Create a GLSurfaceView instance and set it
    mGLView = new OpenGLES10SurfaceView(this);
    setContentView(mGLView);

    //Add the main.xml layout over the OpenGL one
    LayoutInflater inflater = getLayoutInflater();
    View tmpView;
    tmpView = inflater.inflate(R.layout.main, null);
    getWindow().addContentView(tmpView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.FILL_PARENT));

    //Initialize all the buttons and stuff
    The code here is okay, I know it for sure.

    //Allocate enaugh memory for 16 balls(14 regular plus the black and white ball)
    ballSet = new ball[16];

    //Create all the balls needed to play
    for(byte i=0;i<16;i++)
    {
        new ball(i);
    }

}

}

这是 main_menu.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  
    android:paddingLeft="100dp"
    android:paddingTop="80dp"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="@string/newGame" android:layout_marginBottom="40dp" android:layout_marginLeft="10dp"/>



</LinearLayout>

最后但并非最不重要的是我的 LogCat:

    07-10 19:28:42.020: E/AndroidRuntime(350): Uncaught handler: thread main exiting due to uncaught exception
    07-10 19:28:42.032: E/AndroidRuntime(350): java.lang.NullPointerException
    07-10 19:28:42.032: E/AndroidRuntime(350):  at com.desgraff.demoreel.DemoReelActivity.startGame(DemoReelActivity.java:91)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at com.desgraff.demoreel.DemoReelActivity.onClick(DemoReelActivity.java:80)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.View.performClick(View.java:2364)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.View.onTouchEvent(View.java:4179)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.widget.TextView.onTouchEvent(TextView.java:6541)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.View.dispatchTouchEvent(View.java:3709)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.os.Handler.dispatchMessage(Handler.java:99)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.os.Looper.loop(Looper.java:123)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at android.app.ActivityThread.main(ActivityThread.java:4363)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at java.lang.reflect.Method.invokeNative(Native Method)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at java.lang.reflect.Method.invoke(Method.java:521)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    07-10 19:28:42.032: E/AndroidRuntime(350):  at dalvik.system.NativeStart.main(Native Method)

对不起,很长的帖子顺便说一句,但我是 android 的新手(和 Java 相关的)

4

1 回答 1

1

我现在看到了。您需要为 XML 中的根元素提供一个 id 并将此 id 传递给 findViewById(),您无法通过传递整个 XML 文件来加载布局。

首先,将 id 属性添加到您的 LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_menu"
    android:layout_width="fill_parent"
    ...

其次,改变这些:

private View mainMenu;
...
mainMenu =  findViewById(R.layout.main_menu);

像这样:

private LinearLayout mainMenu;
...
mainMenu = (LinearLayout) findViewById(R.id.main_menu);

此外,您将获得 NullPointerException:

mGLView.setVisibility(View.VISIBLE); // Remove me.

// Create a GLSurfaceView instance and set it
mGLView = new OpenGLES10SurfaceView(this);

由于您在尝试使其(无)可见mGLView 进行初始化。

于 2012-07-11T21:22:06.807 回答