Matt Baech
Matt Baech

Reputation: 454

Button onClick error...Could not find a method

I can't seem to start a new Activity from my Button, I have searched here for answers, but can't seem to resolve the problem. I hope someone has an easy solution. Thanks In advance.

Here is the error:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find a method openSearch(View) in the 
activity class dk.mathias.splitcab.MainActivity 
for onClick handler on view class android.widget.Button with id 'btnStartSearch'

MainActivity.java

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

}

public void openSearch(){
    Intent openSearchIntent = new Intent(MainActivity.this, StartSearch.class);
    startActivity(openSearchIntent);
}

AndroidManifest.xml

    <activity
        android:name=".StartSearch"
        android:label="@string/title_activity_start_search" >
        <intent-filter>
            <action android:name="dk.mathias.splitcab.STARTSEARCH" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

activity_main.xml

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/StartSearch"
    android:id="@+id/btnStartSearch"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignRight="@+id/tvWelcome"
    android:layout_alignEnd="@+id/tvWelcome"
    android:onClick="openSearch"

    />

StartSearch.java

public class StartSearch extends Activity {

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

Upvotes: 0

Views: 169

Answers (2)

codeMagic
codeMagic

Reputation: 44571

The problem is in your method signature

public void openSearch(){

it should have one, and only one param, which is a View.

Change it to

public void openSearch(View v){

v obviously can be anything you want it to be but you should make it something meaningful like v, view, etc...

From the Docs

In order for this to work, the method must be public and accept a View as its only parameter.

See this answer for a more detailed description of adding Buttons and OnClick

Upvotes: 6

Srikanth Pai
Srikanth Pai

Reputation: 926

First make a reference to your button

    search = (Button) findViewById(R.id.btnStartSearch);

Then implement the onClick listner for the button as below

        search.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

             Intent openSearchIntent = new Intent(MainActivity.this, StartSearch.class);
             startActivity(openSearchIntent)
        }
    });

Make sure you remove this line from your XML file

 android:onClick="openSearch"

Upvotes: 1

Related Questions