这是我的问题:
在我的应用中,我使用了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)
很抱歉btw的长篇文章,但我是android的新手(和Java一样)
答案 0 :(得分: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
,因此尝试使其(无)可见。