1

我想使用一个按钮将当前活动更改为 android 中的另一个活动。但是,每当我单击该按钮时,eclipse 调试透视图都会出现错误“找不到源”。这是我用来更改活动的功能

public void toManager(){
    Intent i = new Intent(getApplicationContext(), DegreeActivity.class);
    startActivity(i);
}

在我的 xml 文件中,该按钮有一个 onClick 侦听器。这是xml

<Button
    android:id="@+id/btn_toDegree"
    android:text="@string/btn_toDegree"
    android:textSize="13pt"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginLeft="15dip"
    android:layout_marginRight="15dip"
    android:onClick="toManager"  <!-- This line -->
    />  

如果我在第一个活动toManager()的块中调用该函数onCreate(),它会毫无错误地切换到下一个活动。但是,当我尝试使用按钮进行切换时,它不起作用。

4

1 回答 1

7

点击处理程序必须如下所示:

public void toManager(View view) {
    Intent i = new Intent(getApplicationContext(), DegreeActivity.class);
    startActivity(i);
}

按钮文档:

Now, when a user clicks the button, the Android system calls the activity's selfDestruct(View) method. In order for this to work, the method must be public and accept a View as its only parameter.

于 2011-08-27T11:28:18.150 回答