0

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);
    }
}
3
  • Where is your StartSearch.class activity? Oct 29, 2014 at 13:18
  • 1
    so what happens? errors, exceptions, logs ?
    – Yazan
    Oct 29, 2014 at 13:19
  • It looks about right...
    – Alex K
    Oct 29, 2014 at 13:21

2 Answers 2

6

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

0
1

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"
4
  • Why would you suggest this? Everything is fine with the way the OP is doing it besides missing the param in the method
    – codeMagic
    Oct 29, 2014 at 13:26
  • @codeMagic - Your answer is perfectly fine and I have a +1 as well My intension to suggest this is to know the implementation of onClick Listeners Oct 29, 2014 at 13:28
  • 1
    I'm not concerned with the +1, the point is that you make it sound like it is being done wrong with the way the OP is doing it. I see this all the time with onClick. I don't think we should just tell the OP another way without explaining what is being done wrong. It is confusing.
    – codeMagic
    Oct 29, 2014 at 13:30
  • I Chose the other answer for best answer because it was the easiest fix. I Am sure that your solution also works and therefore I have given it an upvote. Thanks for the reply!
    – Matt Baech
    Oct 29, 2014 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.