简体   繁体   English

我不能使用findViewById

[英]I can't use findViewById

Why can't I use findViewById in this file? 为什么我不能在这个文件中使用findViewById

Regfragment : Regfragment:

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class RegFragment extends Fragment {

EditText text1,text2,text3;
Button btn1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.reg_layout, container, false);

    return rootView;


}
}

and then I tried to make like this: 然后我试着这样做:

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class RegFragment extends Fragment {

EditText text1,text2,text3;
Button btn1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.reg_layout, container, false);

    return rootView;

    text1 = (EditText) findViewById(R.id.text1);   -----> ERROR....
}
}

I got error 我收到了错误
Is it because I use Fragments ? 是因为我使用片段吗?

you can not use it in fragment because it's method of activity 你不能在片段中使用它,因为它是活动的方法

try following 尝试下面

in onActivityCreated() 在onActivityCreated()中

text1 = (EditText) getView().findViewById(R.id.text1);

or in onCreateView() 或者在onCreateView()中

text1 = (EditText) rootView.findViewById(R.id.text1);

you have used inflater 你用过inflater

View rootView = inflater.inflate(R.layout.reg_layout, container, false);

so use 所以用

text1 = (EditText) rootView.findViewById(R.id.text1); 
text2 = (EditText) rootView.findViewById(R.id.text2); 
text3 = (EditText) rootView.findViewById(R.id.text3); 

and so on. 等等。

You have to do it before return statement like this way 你必须在返回语句之前这样做

    View rootView = inflater.inflate(R.layout.reg_layout, container, false);
   text1 = (EditText) rootView.findViewById(R.id.text1);
    return rootView;

Hope it will work. 希望它会奏效。

To access the view in the fragment you need to access it through your View class which contains your layout. 要访问片段中的视图,您需要通过包含布局的View类访问它。

You need to change your code as below : 您需要更改以下代码:

public class RegFragment extends Fragment {

EditText text1,text2,text3;
Button btn1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.reg_layout, container, false);
    text1 = (EditText)rootView .findViewById(R.id.text1); 
    //Same way initialize other views also. 
    return rootView;
}
}

Just a simple way. 只是一个简单的方法。 you already inflated the layout. 你已经夸大了布局。 just inherit from that. 只是继承自那。 find the code below 找到下面的代码

View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText)rootView.findViewById(R.id.text1); 

return rootView;

Since you've used a separate View to inflate your layout, you are required to use that view to access all text views/image views/edit text present in that layout. 由于您使用单独的视图来扩充布局,因此您需要使用该视图来访问该布局中存在的所有文本视图/图像视图/编辑文本。

So to correct your code, you would need to tweak your code like shown below 因此,要更正代码,您需要调整代码,如下所示

View rootView = inflater.inflate(R.layout.reg_layout, container, false);
text1 = (EditText) rootView.findViewById(R.id.text1);
return rootView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM