java - 无法在静态上下文中使用它

标签 java android

以下编码有问题。

new AlertDialog.Builder(this)

这个有错误,请帮我看看。

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;
          }
}

最佳答案

这里:

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

是明确不允许的。

因为该方法是 class方法而不是 instance方法,this 实际上并不存在(因为 this 是事物的实际实例)。

这是真正的基本内容,您可能应该阅读一下。 Here's比较两者的链接。

关于java - 无法在静态上下文中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22624976/

相关文章:

java - UTF-8 的不可打印字符 - SUSE Linux Java 不支持

java - 在作业页面上隐藏通过 Spark-Submit 传递的敏感参数

java - eclipse 露娜 - "No projects are found to import"

android - 在 AWS Farm 上运行 appium 测试测试的结果中未运行测试方法

java - 在java中解码md5加密

java - 如何将 Android 设备中的 Whatsapp 联系人与移动联系人分开

java - Android PlaceAutocompleteFragment自动关闭

java - 使用 Android java 应用程序的 Cisco 交换机的 Telnet 客户端

android - Android 后台服务 (Xamarin)

java - for 循环的 onClickListener 不起作用