开发者

button onclicklistener error

i'm getting this error when i try to run my app in the emulator (any android version): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.test/com.android.test.HomeScreen}: java.lang.NullPointerException

my question is: is this because in 2.2 there is no "golf button" in the layout, and in 3.0 there is?

this is my activity:

public class HomeScreen extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homescreen);

    Button basketball = (Button)findViewById(R.id.basketball);

    basketball.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        Intent basketball = new Intent(HomeScreen.this, basketball.class);
        startActivity(basketball);
        }
    });

    Button golf = (Button)findViewById(R.id.golf);

    golf.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        Intent golf = new Intent(HomeScreen.this, golf.class);
        startActivity(golf);
        }
    });
}
}

i got 2 layouts

layout 1: layout-normal-mdpi <= running in 2.2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/homescreen">

    <Button
        android:id="@+id/basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/basketbal"
        android:layout_marginTop="46dp"
        android:layout_marginRight="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true">
    </Button>
</RelativeLayout>

layout 2: xlarge-port <= running in 3.0

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/homescreen">

    <Button
        android:id="@+id/basketball"
        android:background="@drawable/basketbal"
        android:layout_height="225dp"
        android:layout_width="225dp"
        android:layout_marginRight="70dp"
        android:layout_marg开发者_如何学编程inTop="90dp"
        android:layout_alignParentRight="true">
    </Button>
    <Button
        android:id="@+id/golf"
        android:background="@drawable/golf"
        android:layout_height="550dp"
        android:layout_width="211dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="390dp"
        android:layout_alignParentLeft="true">
    </Button>
</RelativeLayout>


if you are getting error in the below line,

Button golf = (Button)findViewById(R.id.golf); 

which is not actually present in the first layout you have provided here, it is no wonder. This definitely doesn't exist and which obviously returns null to you and there you go, getting null pointer exception.

To avoid this either you have to catch the exception else you will have change the layout accordingly such that it contains both the buttons in both the layouts.


You're lacking golf button in your first layout, as you stated. So, when you run

Button golf = (Button)findViewById(R.id.golf);
golf.setOnClickListener(new View.OnClickListener() {

golf is set to null (first line) and then you're trying to assign it something... Try this:

Button golf = (Button)findViewById(R.id.golf);
  if (golf != null) {
    golf.setOnClickListener(new View.OnClickListener() {

EDITED:
Put these lines in your 2.2 layout:

<Button
  android:id="@+id/golf"
  android:visibility="invisible"
  android:layout_height="1dp"
  android:layout_width="1dp"
  android:layout_alignParentLeft="true">
</Button>

With attribute visibility="invisible" your app should work and your layout shouldn't be wasted

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜