Givon Zirkind
Givon Zirkind

Reputation: 31

android onClickListener (newbie)

I copied the following code from a book. And when I run the app, it fails to launch.

The offending line is aboutButton.setOnClickListener(this);. If I knock it out, the app works.

Any clues?

Thanks.

G.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View aboutButton = findViewById(R.id.main_about_button);
    aboutButton.setOnClickListener(this);
    setContentView(R.layout.activity_main);
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center"
    tools:context=".MainMenu"  
    >

    <TextView 
        android:id="@+id/welcome_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="25sp"
        android:text="@string/welcome_title"
        />

    <Button
        android:id="@+id/search_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/search_button"
        />

    <Button
        android:id="@+id/new_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/new_button"
        />

    <Button
        android:id="@+id/help_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/help_button"
        />

    <Button
        android:id="@+id/exit_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit_button"
        />

   <Button 
        android:id="@+id/main_about_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/exit_button"
        android:layout_alignLeft="@+id/exit_button"
        android:text="@string/main_about_button"
        android:clickable="true"
        />

</LinearLayout>

Upvotes: 1

Views: 86

Answers (3)

Givon Zirkind
Givon Zirkind

Reputation: 31

had to put the listener statements AFTER the seContentView command. that fixed it.

my guess is, that the setContentView imports all the variables & ids from the layout. and the findViewById just returned a null pointer or something.

if anyone can confirm my guess, i'd appreciate it.

Upvotes: 1

Matto
Matto

Reputation: 118

I recommend you to declare the aboutButton as a Button. Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button aboutButton = (Button) findViewById(R.id.main_about_button);
    aboutButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Do something
        }
    });

}

I hope it helps!

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

aboutButton is null. You can't initialize it until you have inflated the layout. Change it to

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   // Switch these two lines
    View aboutButton = findViewById(R.id.main_about_button);
    aboutButton.setOnClickListener(this);

}

Your Views exist within your layout which means they will return null if you try to initialize them before inflating your layout, using a layout inflater or by calling setContentView(), which will give you a NPE when trying to call a function on them or setting a listener

Upvotes: 2

Related Questions