2014-03-27 86 views
0

我有这样的方法:如何从另一个类调用super()?

@Override 
public void onBackPressed() { 

    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); 
    alertDialog.setCancelable(false); 
    alertDialog.setTitle("Sair"); 
    alertDialog.setMessage("Do you wanna leave?"); 
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
       super.onBackPressed(); 
      }     
     }); 

    alertDialog.show(); 
} 

这种方法使我有以下错误:

The method onBackPressed() is undefined for the type Object. 

我怎么能称之为超级方法?

回答

1

您可以使函数静态并静态调用它,或者您可以获取它的实例(可能需要使其成为单例)并使用它。

3

您需要限定封闭类:EnclosingClass.super.onBackPressed();我并非100%确定该方法适用于super,但它适用于this

+0

它适用于超级。 – CaptRisky