【问题标题】:non-static method <T>findViewById(int) cannot be referenced from a static context不能从静态上下文引用非静态方法 <T>findViewById(int)
【发布时间】:2018-12-15 16:14:54
【问题描述】:

我的第一个应用程序单独运行良好,但现在我尝试按照教程添加选项卡,但我卡住了。 我一直在搜索,很多用户都有同样的问题,我尝试了这些解决方案,但仍然无法正常工作。

我的应用

package es.ea1ddo.antenacubica;

import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        frecuencia = findViewById(R.id.frecuencia);
        cal3el = (Button) findViewById(R.id.calcular3);

现在这是我试图复制以前的应用程序的地方

package es.ea1ddo.calculadoraantenascubicas;

public class TabFragment2 extends Fragment {

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

        frecuencia = EditText.findViewById(R.id.frecuencia);
        cal3el = (Button) getView().findViewById(R.id.calcular3);

我一直在四处寻找,尝试了许多示例和不同的方法,但我被困住了。 你可以看到我添加了getView()。在每个 findViewById 之前,但我仍然遇到同样的错误:

非静态方法 findViewById(int) 不能从 T 是类型变量的静态上下文中引用: T 扩展了方法 findViewById(int) 中声明的 View

请给点建议? 谢谢

【问题讨论】:

  • 您需要将EditText.findViewById 替换为(EditText) getView().findViewById
  • 感谢流浪者。您的修复已删除该错误。实际上运行没有错误但是...应用程序崩溃了,我可以在下一个调试中看到: PID: 12271 java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int )' 在空对象引用上。针对同一行:frecuencia = (EditText) getView().findViewById(R.id.frecuencia);
  • 使用 v.findViewById 代替 getView()

标签: java android class findviewbyid


【解决方案1】:

在您的片段代码中,您有两个问题。

第一个,正如其他人所指出的,View.findViewById() 是一个非静态方法,因此您可以像 myView.findViewById() 那样调用它,而不是 EditText.findViewById()

第二个与 Fragment 的 getView() 方法的工作原理有关。此方法仅在onCreateView() 返回之后 有效,因为getView() 返回onCreateView() 返回的任何内容。这意味着您不能从onCreateView() 中调用getView();它总是会返回null

综合起来,您的代码应该如下所示:

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

    frecuencia = (EditText) v.findViewById(R.id.frecuencia);
    cal3el = (Button) v.findViewById(R.id.calcular3);
    ...
}

【讨论】:

  • 本,你就是男人。它现在正在工作!顺便说一句,所有 (EditText) 和 (Button) 都是灰色的,显示“Casting ... 是多余的”,所以我删除了并且仍然可以正常工作。唯一变灰的是这一行中的“inflater”:public View onCreateView(LayoutInflater inflater,... 显示“Not annotated parameter overrides @NonNull parameter”。可以吗?
  • 是的,没关系。它告诉您您正在覆盖使用@NonNull LayoutInflater inflater 定义的方法,但您只输入了LayoutInflater inflater。您可以根据需要添加注释,也可以忽略警告。
【解决方案2】:

替换

frecuencia = EditText.findViewById(R.id.frecuencia);
cal3el = (Button) getView().findViewById(R.id.calcular3);

recuencia = (EditText) v.findViewById(R.id.frecuencia);
cal3el = (Button) v.findViewById(R.id.calcular3);

如果不起作用,请在您的问题中添加完整的 xml 代码

【讨论】:

    【解决方案3】:

    替换

    EditText.findViewById
    

    (ExitText) getView().findViewById
    

    但是,这将导致 NullPointerException。

    您需要将任何使用getView() 的代码移动到onViewCreated(),或者改为引用v

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-07
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多