简体   繁体   中英

Android button onClick cannot find associated method in xml

I am trying to have a button activate a method to call a url but keep getting an error

Here is the main activity xml with a button to call the next activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" > 

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="76dp"
        android:text="@string/main_button_lights"
        android:onClick="onLightClicked"/>
</RelativeLayout>

And its java file

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;    
public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void onLightClicked(View view)
    {
         setContentView(R.layout.activity_set_lights);
    }           
}

When the above button is clicked it opens up the following activity with no problems:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SetLightsActivity" >  

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="21dp"
        android:text="ToggleButton"
        android:onClick="onLightOn"
         />
</RelativeLayout>

When I click the button that calls the onLightOn method here

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ToggleButton;

public class SetLightsActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.set_lights, menu);
        return true;
        }   
    public void onLightOn(View view) 
    {
        // Is the toggle on?
        boolean on = ((ToggleButton) view).isChecked();

        if (on) 
          {
            Uri uri = Uri.parse("http://192.168.0.100/cgi-bin/cgiRelayOn.cgi");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
          }     

        else 
            {
              Uri uri = Uri.parse("http://192.168.0.100/cgi-bin/cgiRelayOff.cgi");
              Intent intent = new Intent(Intent.ACTION_VIEW, uri);
              startActivity(intent);
            }
     }  
    }

it crashes with the following error

06-02 10:15:29.941: E/AndroidRuntime(1140): FATAL EXCEPTION: main
06-02 10:15:29.941: E/AndroidRuntime(1140): java.lang.IllegalStateException: Could not find a method onLightOn(View) in the activity class com.flynn85.homecontrol.MainActivity for onClick handler on view class android.widget.ToggleButton with id 'toggleButton1'
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.view.View$1.onClick(View.java:3620)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.view.View.performClick(View.java:4240)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.widget.CompoundButton.performClick(CompoundButton.java:100)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.view.View$PerformClick.run(View.java:17721)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.os.Handler.handleCallback(Handler.java:730)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.os.Looper.loop(Looper.java:137)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invokeNative(Native Method)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at java.lang.reflect.Method.invoke(Method.java:525)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at dalvik.system.NativeStart.main(Native Method)
06-02 10:15:29.941: E/AndroidRuntime(1140): Caused by: java.lang.NoSuchMethodException: onLightOn [class android.view.View]
06-02 10:15:29.941: E/AndroidRuntime(1140):     at java.lang.Class.getConstructorOrMethod(Class.java:423)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at java.lang.Class.getMethod(Class.java:787)
06-02 10:15:29.941: E/AndroidRuntime(1140):     at android.view.View$1.onClick(View.java:3613)
06-02 10:15:29.941: E/AndroidRuntime(1140):     ... 12 more

If I copy the method into the main activity.java it will work,but I need it in the corresponding activity file Can anyone help me thanks

I'd rather do the following:

public class SetLightsActivity extends Activity implements View.OnClickListener {

private ToggleButton toggleButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_lights);
    this.toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
    this.toggleButton.setOnClickListener(this);
}

....
@Override
public void onClick(View view) 
{
  if(view == toggleButton) 
  {
    boolean on = toggleButton.isChecked();

    if (on) 
    {
        Uri uri = Uri.parse("http://192.168.0.100/cgi-bin/cgiRelayOn.cgi");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    } 
    else 
    {
          Uri uri = Uri.parse("http://192.168.0.100/cgi-bin/cgiRelayOff.cgi");
          Intent intent = new Intent(Intent.ACTION_VIEW, uri);
          startActivity(intent);
    }
   }
 }
}

Unless it is explicitly specified that you MUST use the XML to specify the onClick listener.

EDIT: By the way, you should start your second activity using an Intent, rather than just setting the content of your initial MainActivity to look like the second Activity.

Your onLightClicked function should be the following:

 Intent intent = new Intent(this, SetLightsActivity.class);
 startActivity(intent);

It's trying to find the onLightOn(View view) method on the MainActivity class. Since you have it on the SetLightsActivity class, the JVM can't find it

If you declared the method in MainActivity xml it will only apply in that class.

If you want to invoke method on other activity you can call a method in your MainActivity which invokes your second activity method (you can make it static).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM