Kar Heng
Kar Heng

Reputation: 1

Cannot use this in a static context

having problem on below coding.

new AlertDialog.Builder(this)

the this having error, please help me have a look.

import java.io.File;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {


        public static boolean isPhoneRooted() {

            // get from build info
            String buildTags = android.os.Build.TAGS;
            if (buildTags != null && buildTags.contains("test-keys")) {
              return true;
            }

            // check if /system/app/Superuser.apk is present
            try {
              File file = new File("/system/app/Superuser.apk");
              if (file.exists()) {

                  new AlertDialog.Builder(this)

                    .setIcon(R.drawable.ic_launcher)

                    .setTitle("[" + file.getName() + "] folder can't be read!")

                    .setPositiveButton("OK", 

                      new DialogInterface.OnClickListener() {     

                       @Override

                       public void onClick(DialogInterface dialog, int which) {

                        // TODO Auto-generated method stub

                       }

                      }).show();

              }
            } catch (Throwable e1) {
              // ignore
            }

            return false;
          }
}

Upvotes: 0

Views: 1785

Answers (2)

Sujith Thankachan
Sujith Thankachan

Reputation: 3506

Change the public static boolean isPhoneRooted() to public static boolean isPhoneRooted(Context context) and replace the code using this keyword with the context parameter of the method. And call the isPhoneRooted(); method as isPhoneRooted(this);

Upvotes: 0

Pedantic
Pedantic

Reputation: 5022

Here:

public static boolean isPhoneRooted() {
  ...
  new AlertDialog.Builder(this)
  ....
}

is explicitly not allowed.

Because the method is a class method and not an instance method, this doesn't actually exist (because this is an actual instance of a thing).

This is real basic stuff, you should probably read up on it. Here's a link comparing the two.

Upvotes: 1

Related Questions