无法解析findByViewId [重复]

问题描述 投票:0回答:1

此问题已经在这里有了答案:

我想在一个片段中生成一个表,需要找到View。但这是行不通的。这是代码

  public void genTabelle(View v){
        for(int i=1; i<12; i++){
            //finding the table
        TableLayout tl = (TableLayout) findByViewId(R.id.tableLayout2);
       }
    }

和我的进口货

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.view.View.*;

import java.util.Objects;



错误消息是:“无法解析方法'findById(int)'”

我认为我错过了进口商品,但不确定。

android android-tablelayout
1个回答
0
投票

findViewById()函数是ActivityView类的一部分,但不是Fragment类的一部分。您需要做的是在o onCreateView中获取膨胀视图的引用,并在该视图上调用findViewById()

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.example_fragment, container, false);
         view.findViewById() //assign to a reference
          return view;

    }
© www.soinside.com 2019 - 2024. All rights reserved.