gpt4 book ai didi

java - android studio 局部变量冗余

转载 作者:行者123 更新时间:2023-12-01 11:20:15 25 4
gpt4 key购买 nike

有人可以向我解释为什么它给我“局部变量是多余的错误”吗?

package com.example.smite.floater;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.zip.Inflater;

public class creator extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.creator ,container,false);
return v;
}
}

一开始,仅在 R 上出现不同的错误但在我更改 xml 后,发生了这种情况

最佳答案

在你的方法中,你是:

  • 返回一个值,然后创建一个变量。这是一个错误,因为这段代码永远不会运行

  • 您正在创建一个未被使用的变量

您的代码:

public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
// It is after a return statement!
View v = inflater.inflate(R.layout.creator ,container,false);
//You are creating a variable which is not being used
return v;
}

用途:

public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.creator ,container,false);
}

关于java - android studio 局部变量冗余,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31324233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com