java 方法不会覆盖其超类中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36896739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method does not override method from its superclass
提问by NiklasZeroZero
Can anyone tell me, why this error is displayed (or even have a better solution)?
I'm new to Java (and Android) development, so I realy don't know my fault
My Code:
谁能告诉我,为什么会显示此错误(甚至有更好的解决方案)?
我是 Java(和 Android)开发的新手,所以我真的不知道我的错我的代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class select_type extends Fragment {
public select_type() {
// Required empty public constructor
}
@Override //<--- Cause the ERROR
public View onCreateView(View view, LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
return inflater.inflate(R.layout.fragment_select_type, container, false);
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noVIP TextFragment = new noVIP();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, TextFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
return view;
}
}
采纳答案by Kanchan Chowdhury
You can override two methods also:
您也可以覆盖两个方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_select_type, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noVIP TextFragment = new noVIP();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, TextFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
回答by CommonsWare
Replace:
代替:
public View onCreateView(View view, LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
with:
和:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
as onCreateView()
is not passed a View
parameter.
Also, get rid of super.onViewCreated(view, savedInstanceState);
. It is neither necessary nor appropriate to be calling that where you are.
此外,摆脱super.onViewCreated(view, savedInstanceState);
. 在你所在的地方调用它既没有必要也不合适。
Please note that everything after your return
statement in that method will not be executed, since you are returning first. Assign the inflater.inflate(...)
result to a local variable, use it, then return that variable at the end of your method.
请注意,return
该方法中的语句之后的所有内容都不会执行,因为您首先返回。将inflater.inflate(...)
结果分配给局部变量,使用它,然后在方法结束时返回该变量。