我如何解决无法解决方法的findViewById(int)错误 [英] How do I resolve the the cannot resolve method findViewById(int) error
问题描述
我有一个包含以下属性的布局文件;
I have a layout file with the following attributes;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/cityDialog"
android:inputType="textCapWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/city_hint" />
<EditText
android:id="@+id/stateDialog"
android:inputType="textCapWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/state_hint"/>
<EditText
android:id="@+id/countryDialog"
android:inputType="textCapWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/country_hint"/>
</LinearLayout>
然后我有一个相应的类,声明如下:
Then I have a corresponding class with the following declarations;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.R.*;
public class settings extends DialogFragment{
public static final String IMAGE_RESOURCE_ID = "iconResourceID";
public static final String ITEM_NAME = "itemName";
private EditText city = (EditText) findViewById(R.id.cityDialog);
private EditText state;
private EditText country;
public settings(){}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_setings, container, false);
}
}
但是我得到了一个
But I'm getting a
Cannot resolve method 'findViewById(int)'
和我不知道为什么。
我试过的东西;
- 删除R文件这样就可以生成资源
- 清理和重建整个项目
- 重命名属性
但似乎没有任何效果。我错过了什么?
and I'm not sure why.
Things I've tried;
- Removing the R file so that the resource can be generated
- Cleaning and rebuilding the entire project
- Renaming the attributes
But nothing seems to be working. What am I missing?
推荐答案
你需要先调用getView()
函数来获取视图片段,然后你可以调用findViewById()
。
喜欢这个,
You need to first call thegetView()
function to get the view of the fragment and then you can call thefindViewById()
.
Like this,
private EditText city = (EditText) getView().findViewById(R.id.cityDialog);
现在它应该可以工作。或者您可以使用 getActivity()
作为片段,因为您无法在片段或DialogFragment中直接调用 findViewById()
等等。此外,您应该在 onActivityCreated()
事件中检查并执行这些功能。
Now it should work. Or you can use the getActivity()
for the fragment because you cannot directly call findViewById()
in a Fragment or DialogFragment and so on. Also, you should check and execute these functions in onActivityCreated()
event.
这篇关于我如何解决无法解决方法的findViewById(int)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!