开发者

Syntax error on 'onBackPressed()'

I am displaying a dialog box. When the back key pressed, I am writing a code and using back key default functionality, but every time I get following error, here is my code:

public void myfunc {
        new AlertDialog.Builder(TaxiPlexer.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Enable GPS").setMessage(
                "Please Enable GPS in device settings to use Taxi application").setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                 //do something
            } // onClick                

        }).show();

        @Override //error: Syntax error on token(s), misplaced construct(s)
        public void onBackPressed() {   //error: Syntax error on token "void", @ expected       
            GPSdialog = fal开发者_StackOverflow社区se;
            super.onBackPressed();
        } }


This is the correct syntax. You override a method. So you have to place the method in a class and not in another method.

public void myfunc {
        new AlertDialog.Builder(TaxiPlexer.this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Enable GPS").setMessage(
                "Please Enable GPS in device settings to use Taxi application").setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                 //do something
            } // onClick                

        }).show();
}


        @Override //error: Syntax error on token(s), misplaced construct(s)
        public void onBackPressed() {   //error: Syntax error on token "void", @ expected       
            GPSdialog = false;
            super.onBackPressed();
        } 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜