0
我得到一個未經檢查的投射警告,儘管查看了許多StackOverflow帖子,似乎無法解決此問題。什麼是做這個演員的正確,安全的方法?在此先感謝:-)未經檢查的投射警告
/**
* Fragment for retaining data across screen orientation changes
*/
public class RetainedFragment<T> extends Fragment {
public T data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
public static <T> RetainedFragment<T> findOrCreate(FragmentManager fm, String tag) {
// THIS LINE I GET AN UNCHECKED CAST WARNING
RetainedFragment<T> retainFragment = (RetainedFragment<T>) fm.findFragmentByTag(tag);
if(retainFragment == null){
retainFragment = new RetainedFragment<>();
fm.beginTransaction()
.add(retainFragment, tag)
.commitAllowingStateLoss();
}
return retainFragment;
}
}
我試過: 片段片段= fm.findFragmentByTag(標籤); if(片段instanceof RetainedFragment){ RetainedFragment retainFragment =(RetainedFragment )fragment; } 但它仍然給出相同的警告。 –
[什麼是Java中的SuppressWarnings(「unchecked」)?](https://stackoverflow.com/q/1129795/823393) - 但只有在您確定的情況下。 – OldCurmudgeon