【问题标题】:Error when I check for internet connection检查互联网连接时出错
【发布时间】:2019-05-26 11:06:27
【问题描述】:

我真的是 android 新手,我正在开发一个需要检查设备是否连接到互联网的应用程序。这是我不断遇到的错误。

java.lang.IllegalStateException:系统服务不可用于 android.app.Activity.getSystemService 的 onCreate() 之前的活动

public class Fragment_Home extends Fragment implements Class_Home_Adapter.OnItemClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
    readJsonFile();
return v;
}

private void readJsonFile() { 
        Class_Check_Internet_Connection class_check_internet_connection = new Class_Check_Internet_Connection();
        Log.e(TAG, "Internet Status: " + class_check_internet_connection.internetStatus());
}

这是我用来检查互联网连接的类

public class Class_Check_Internet_Connection extends Activity {
    public static boolean isNetworkAvailable(Context context){
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if ((activeNetworkInfo != null)&&(activeNetworkInfo.isConnected())){
            return true;
        }else{
            return false;
        }
    }
    public boolean internetStatus() {
        boolean isNetworkAvailable = Class_Check_Internet_Connection.isNetworkAvailable(this);
        return isNetworkAvailable;
    }
}

【问题讨论】:

标签: java android


【解决方案1】:

没有理由在您的Class_Check_Internet_Connection 类中“扩展活动”。看来您正在课堂上扩展Activity——可能试图强制创建ContextContext 是在 onCreate() 方法中创建的。但是您“绕过”了Context 的创建,因为没有onCreate() 方法...所以这是空的!

但是您不需要创建Context- - 你也不应该。你不应该为这个类扩展Activity!而是将Context 作为参数传递给类。

// Snake case is generally not used with Jave, but I will leave it...
public class Class_Check_Internet_Connection {

    Context mContext = null;
    public Class_Check_Internet_Connection(Context context){
        this.mContext = context;
    }

    public static boolean isNetworkAvailable(){
        ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if ((activeNetworkInfo != null)&&(activeNetworkInfo.isConnected())){
            return true;
        }else{
            return false;
        }
    }

    // another public (!) method to simply query isNetworkAvailable?? If you think you need it....
    public boolean internetStatus() {
        boolean isNetworkAvailable = Class_Check_Internet_Connection.isNetworkAvailable();
        return isNetworkAvailable;
    }
}

// Strange name for a method that checks the internet connectivity!!
private void readJsonFile() { 
        Class_Check_Internet_Connection class_check_internet_connection = new Class_Check_Internet_Connection(this);
        Log.e(TAG, "Internet Status: " + class_check_internet_connection.internetStatus());
}




注意:
我没有检查您的代码以查看它是否正常运行。我只是解决您收到的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2017-03-16
    • 2014-02-08
    • 2012-06-12
    • 1970-01-01
    相关资源
    最近更新 更多