Android按钮onClick无法在xml中找到关联的方法

时间:2014-06-02 14:30:37

标签: java android xml onclick runtime-error

我正在尝试使用按钮激活方法来调用网址但不断出现错误

以下是主要活动xml,其中包含一个用于调用下一个活动的按钮

<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>

及其java文件

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);
    }           
}

单击上面的按钮时,它会打开以下活动,没有任何问题:

<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>

单击此处调用onLightOn方法的按钮

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);
            }
     }  
    }

崩溃时出现以下错误

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

如果我将方法复制到main activity.java中,它会起作用,但我需要在相应的活动文件中 谁能帮我 感谢

3 个答案:

答案 0 :(得分:2)

我宁愿做以下事情:

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);
    }
   }
 }
}

除非明确指出您必须使用XML来指定onClick侦听器。

编辑: 顺便说一句,您应该使用Intent开始第二个活动,而不是仅仅将初始MainActivity的内容设置为第二个Activity。

您的onLightClicked功能应如下所示:

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

答案 1 :(得分:0)

它试图在onLightOn(View view)类上找到MainActivity方法。 由于您在SetLightsActivity类上拥有它,因此JVM无法找到它

答案 2 :(得分:0)

如果您在MainActivity xml中声明了该方法,它将仅适用于该类。

如果要在其他活动上调用方法,可以在MainActivity中调用一个方法来调用第二个活动方法(可以将其设置为静态)。