I am using Android Studio (Beta), and while using this java code in 'onCreateView()', I get an error.
我使用的是Android Studio (Beta),在“onCreateView()”中使用这个java代码时,我得到了一个错误。
ListView listView = (ListView) findViewById(R.id.someListView);
This is the error:
这是错误的:
Non-static method 'findViewById(int)' cannot be referenced from a static context
How do I fix this?
我怎么修复这个?
5 个解决方案
#1
25
Assuming you have a static fragment inner class inside an activity: you're trying to call the activity's findViewById()
which you cannot in a static inner class that doesn't hold a reference to the parent.
假设您在一个活动中有一个静态片段内部类:您正在尝试调用活动的findViewById(),而在一个不包含父类引用的静态内部类中,您不能调用它。
In onCreateView()
you need to call it on the root view you just inflated, e.g.
在onCreateView()中,你需要在你刚刚膨胀的根视图中调用它。
ListView listView = (ListView) rootView.findViewById(R.id.someListView);
#2
2
onCreateView()
shouldn't be a static method (I'm assuming you are defining it within an Activity class), so you must be doing something wrong.
onCreateView()不应该是一个静态方法(我假设您在活动类中定义它),所以您一定做错了什么。
#3
0
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_layout, container, false);
ListView listView = (ListView) view.findViewById(R.id.listviewID);
context = getActivity();
return view;
}
You can implement this now... Use view variable to access xml ui things in oncreateView and getActivity().findViewById(...) to access other then onCreateView() Method.
您现在可以实现这个……使用视图变量访问oncreateView和getActivity(). findviewbyid(…)来访问其他oncreateView()方法。
#4
0
Inside Activity class
在Activity类
static ListView listView;
listView = (ListView) this.findViewById(R.id.someListView);
OR
或
to create inside fragmentClass(static)
创建内部fragmentClass(静态)
static ListView listView;
listView = (ListView) getActivity().findViewById(R.id.someListView);
#5
0
If you are using it in an static AsyncTask, or any other class, you can pass the activity as a parameter to a method or constructor, for example:
如果您在静态异步任务或任何其他类中使用它,您可以将活动作为参数传递给方法或构造函数,例如:
activity onCreate:
活动onCreate:
//Pass activity variable (this)
new Main2Activity.MyTask().execute(this);
class inside activity:
内部类活动:
private static class MyTask extends AsyncTask<Object, Void, String> {
Main2Activity activity;
@Override
protected String doInBackground(Object... params) {
activity = (Main2Activity)params[0];
....
}
@Override
protected void onPostExecute(String str) {
// Use parameter activity passed to class
WebView webView = activity.findViewById(R.id.web_view);
webView.loadData(str, "text/html; charset=UTF-8", null);
}
#1
25
Assuming you have a static fragment inner class inside an activity: you're trying to call the activity's findViewById()
which you cannot in a static inner class that doesn't hold a reference to the parent.
假设您在一个活动中有一个静态片段内部类:您正在尝试调用活动的findViewById(),而在一个不包含父类引用的静态内部类中,您不能调用它。
In onCreateView()
you need to call it on the root view you just inflated, e.g.
在onCreateView()中,你需要在你刚刚膨胀的根视图中调用它。
ListView listView = (ListView) rootView.findViewById(R.id.someListView);
#2
2
onCreateView()
shouldn't be a static method (I'm assuming you are defining it within an Activity class), so you must be doing something wrong.
onCreateView()不应该是一个静态方法(我假设您在活动类中定义它),所以您一定做错了什么。
#3
0
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_layout, container, false);
ListView listView = (ListView) view.findViewById(R.id.listviewID);
context = getActivity();
return view;
}
You can implement this now... Use view variable to access xml ui things in oncreateView and getActivity().findViewById(...) to access other then onCreateView() Method.
您现在可以实现这个……使用视图变量访问oncreateView和getActivity(). findviewbyid(…)来访问其他oncreateView()方法。
#4
0
Inside Activity class
在Activity类
static ListView listView;
listView = (ListView) this.findViewById(R.id.someListView);
OR
或
to create inside fragmentClass(static)
创建内部fragmentClass(静态)
static ListView listView;
listView = (ListView) getActivity().findViewById(R.id.someListView);
#5
0
If you are using it in an static AsyncTask, or any other class, you can pass the activity as a parameter to a method or constructor, for example:
如果您在静态异步任务或任何其他类中使用它,您可以将活动作为参数传递给方法或构造函数,例如:
activity onCreate:
活动onCreate:
//Pass activity variable (this)
new Main2Activity.MyTask().execute(this);
class inside activity:
内部类活动:
private static class MyTask extends AsyncTask<Object, Void, String> {
Main2Activity activity;
@Override
protected String doInBackground(Object... params) {
activity = (Main2Activity)params[0];
....
}
@Override
protected void onPostExecute(String str) {
// Use parameter activity passed to class
WebView webView = activity.findViewById(R.id.web_view);
webView.loadData(str, "text/html; charset=UTF-8", null);
}