here`s my problem:
In my app, I use 2 layouts; one being for the main menu and the other one is the actual in-game layout. When I start my game, I want the main-menu layout the be removed. I thought about doing so using
myView.setVisibility(View.GONE);
And then make the other layout using the same method.
The problem is that when I try to make the layout disappear, my app closes and the message box telling me so appears.
EDIT: The folowing code has changed
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);
}
}
}
This is the main_menu.xml file:
<?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>
And last but not least here is my 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)
Sorry for the long post btw but I am new to android(and Java for that matter)
I see it now. You need to give the root element in you XML an id and pass this id to findViewById(), you cannot load a layout by passing the whole XML file.
First, add the id attribute to your LinearLayout:
Second, change these:
like so:
Also, you are about to get a NullPointerException:
Since you are initializing
mGLViewafter you try to make it (nothing) visible.