android studio错误
1个回答
展开全部
创建一个newInstanc
public class TestFragment extends Fragment
{
private String name;
private String passwd;
public static TestFragment newInstance(String name, string passwd) {
TestFragment newFragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putString("passwd", passwd);
newFragment.setArguments(bundle);
return newFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.main, null);
return view;
}
}
,这个错误就消失了,在Fragment所依赖的Activity中,用以下语句创建Fragment实例即可
Fragment testFragment=TestFragment.newInstance(“name”,”passwd”);
//对于从Activity传递到Fragment中的参数只需要在Fragment的onCreate中获取就可以了
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
name = args.getString("name");
passwd = args.getstring("passwd");
}
}
public class TestFragment extends Fragment
{
private String name;
private String passwd;
public static TestFragment newInstance(String name, string passwd) {
TestFragment newFragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putString("passwd", passwd);
newFragment.setArguments(bundle);
return newFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.main, null);
return view;
}
}
,这个错误就消失了,在Fragment所依赖的Activity中,用以下语句创建Fragment实例即可
Fragment testFragment=TestFragment.newInstance(“name”,”passwd”);
//对于从Activity传递到Fragment中的参数只需要在Fragment的onCreate中获取就可以了
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
name = args.getString("name");
passwd = args.getstring("passwd");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询