简体   繁体   English

不安全或未经检查的操作警告

[英]Unsafe or unchecked operations warning

In an app I am using SharedPrefernces to save/load (serialize/deserialize) some objects.在一个应用程序中,我使用 SharedPrefernces 来保存/加载(序列化/反序列化)一些对象。

This is the deserialization code:这是反序列化代码:

private void loadData() {

    String str = sharedPreferences.getString(PREF_TAG, null);

    byte[] bytes = Base64.decode(str, Base64.DEFAULT);

    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream input = new ObjectInputStream(bais);
        arrayOfObjects = (ArrayList<MyObject>) input.readObject();
    } catch (Exception e) {
        Log.i("BUG", "error decoding serialized objects: " + e.toString());
    }

    if (arrayOfObjects == null) {
        Log.i("BUG", "serious problem!");
    }

}

But whenever I compile this project, the line:但是每当我编译这个项目时,该行:

arrayOfObjects = (ArrayList<MyObject>) input.readObject();

causes a warning that the class containing this method "uses unchecked or unsafe operations."导致包含此方法的 class“使用未经检查或不安全的操作”的警告。

How can I get rid of this warning or change my code to be more safe?我怎样才能摆脱这个警告或更改我的代码以更安全?

In this case that warinig is shown because you are parsing directly the result of在这种情况下,显示 warinig 是因为您正在直接解析结果

input.readObject();

That returns an Object of type Object, (that is so generic), into an ArrayList, and the compiler tries to say you that, it could be any other type of objects.这将返回一个 Object 类型的 Object(非常通用)到一个 ArrayList 中,编译器试图告诉你,它可以是任何其他类型的对象。

In my opinion its not an important warning if that instruction in your code is always returning ArrayList, so i would add to your method definition.在我看来,如果您的代码中的指令总是返回 ArrayList,这不是一个重要的警告,所以我会添加到您的方法定义中。

@SuppressWarnings("unchecked")

I had the same problem and solved it by adding :我遇到了同样的问题并通过添加解决了它:

@SuppressWarnings("unchecked")
public class className extends AppCompatActivity {
...
}

I hope this will help我希望这个能帮上忙

I solved this problem with add :我用 add 解决了这个问题:

@SuppressWarnings("unchecked")
public class CommLockInfoManager {

To everyone who gave answer earlier.对于早先给出答案的每个人。 If you write如果你写

@SuppressWarnings("unchecked")

You tell to java: "Please not check what is under the command."你告诉 java:“请不要检查命令下的内容。” This is not solution this error.这不是解决此错误的方法。 If you want to the solve the problem you must don't use interface and select some name class.如果你想解决问题,你一定不要使用接口和 select 一些名称 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM