开发者

simple Java problem [closed]

开发者 https://www.devze.com 2023-02-26 02:44 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Example 1 - works

public class MainScreen extends Activity implements OnClickListener {

    public void onClick(View v) {
        Button touchedButton = (Button) v;

        Test (MainScreen.this, touchedButton.getId());
         }

  开发者_运维问答  public  void Test (MainScreen mainscreen, int touchedButton) {

    if (touchedButton == R.id.serviceButton )
    startService(new Intent(mainscreen, SimpleService.class));

    if (touchedButton == R.id.cancelButton)
    stopService(new Intent(mainscreen, SimpleService.class));
}
}

Example 2 – does not work

public class MainScreen extends Activity implements OnClickListener {

    public void onClick(View v) {
        Button touchedButton = (Button) v;

        Secondary.Test (MainScreen.this, touchedButton.getId());
          }
}


public class Secondary  extends Activity {


public  void Test (MainScreen mainscreen, int touchedButton) {

    if (touchedButton == R.id.serviceButton )
    startService(new Intent(mainscreen,SimpleService.class));

    if (touchedButton == R.id.cancelButton)
    stopService(new Intent(mainscreen,SimpleService.class));
         }
}

Why does Example 2 not work?


You're trying to call a non-static method statically on this line:

Secondary.Test (MainScreen.this, touchedButton.getId());

You need to create an instance of Secondary and call Test() on the instance or change Secondary.Test() to be a static method like this:

public static void Test (MainScreen mainscreen, int touchedButton) {
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号