我不知道这个问题是什么。但它告诉我“canont resolve方法”findviewbyid(int)'“。我的代码告诉我如何修复它 [英] I don't know whats the problem.but it shows me "cannont resolve method 'findviewbyid(int)'".here is my code tell me how to fix it

查看:416
本文介绍了我不知道这个问题是什么。但它告诉我“canont resolve方法”findviewbyid(int)'“。我的代码告诉我如何修复它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package com.example.afinal;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.widget.TextView;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by admin on 13/07/2017.
 */

public class Tab1 extends Fragment {
    TextView textView;
    TextView label1;
    String  strDate;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/dd/MM");

        Date day = new Date();
        strDate=sdf.format(new Date(day.getTime() - TimeUnit.DAYS.toMillis(1)));
        textView =(TextView)findViewById(R.id.textView);
        textView.setText(strDate);
        return inflater.inflate(R.layout.tab1,container,false);

        //  textView.setText(new Date().toString());
    }
}





我的尝试:



我尝试在findviewById(int)之前使用getView()方法。我的应用程序在使用该方法后崩溃了。这是一个初学者在android.Help me out Guys。



What I have tried:

I tried using getView() method before findviewById(int).My app crashed after using that method.Am an beginner in android.Help me out Guys.

推荐答案

这里要理解的几件事情,首先你要使用 findViewById ,但是在 Fragment 派生类,其次你在 onCreateView()函数中使用它。这些是不允许的,您需要等待并从以后的函数(任何其他处理程序)调用它。一个快速的黑客(和一个不好的做法)是认为你可以膨胀一个新的视图并从那里捕获对象,只是不要这样做。



最好的方法是调用任何其他函数,例如 onStart()函数或任何其他函数向用户显示View时执行的功能。向用户显示正在加载...并不是一个坏习惯。



Several things to understand here, first of all you are using findViewById, but in the Fragment derived class, secondly you are using it in the onCreateView() function. These are not allowed and you need to wait and call it from a later function (any other handler). A quick hack (and a bad practice) would be to think that you can just inflate a new view and capture the objects from there, just don't do that.

The best approach would be to call it any other function, such as onStart() function or any other function that gets executed when your View is shown to the user. It won't be a bad practice to show a "Loading..." to the user.

public void onStart() {
    super.onStart(); // Call this here. 
    // Get it here.
    TextView textView = (TextView) getView().findViewById(R.id.textView);
}



注意事实上,从不保证会调用onViewCreated()函数,因此这个函数会很有用。更多信息,您应该考虑阅读 Fragment [ ^ ]类参考了解如何调用函数 - 事件被引发。



android - findViewById in Fragment - Stack Overflow [ ^ ]


Notice the fact that onViewCreated() function is never guaranteed to be called thus this function would be useful. For more, you should consider reading the Fragment[^] class reference to understand how functions get called — events get raised.

android - findViewById in Fragment - Stack Overflow[^]


这篇关于我不知道这个问题是什么。但它告诉我“canont resolve方法”findviewbyid(int)'“。我的代码告诉我如何修复它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆