NullPointerException when creating onClickListener



Okay, I've created a very basic Android game. The simple idea is that when the user clicks the jump button, it calls the jump method. I'm using a XML file to declare the button, but I'm finding it throws a NullPointerException.


I've looked at other answers, and I realise that the most likely reason is because the button id isn't in the XML file (at least when it's being initialised). I've tried changing my code to match other answers here, but it's still throwing the exception.


Would anyone be able to take a look and just see? I'm fairly new (and hopeless) with anything Android, and would really appreciate the help.




The code (GameActivity.java):

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
theView = (GameView)findViewById(R.id.gameView);
//Create jump button and link to jump event
Button jumpButton = (Button)theView.findViewById(R.id.jumpbutton);
jumpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
//jump
theGame.getPlayer().jump();
}
});


The XML (main.xml):



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.RoundHouse.GoGoRunner.GameView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gameView"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jumpbutton"
android:layout_marginRight="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" android:clickable="true" android:background="@drawable/jumpbutton"/>
</RelativeLayout>


Again, apologies for asking what probably seems like a trivial question, but I've tried other Stack Overflow solutions with no positive outcome.


No comments:

Post a Comment