我创建测试空片段。它没有任何元素,返回 null 。然后我运行应用程序。在最初的方向,一切都很好。
static FragmentTransaction fragment_trans;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableClassFragment tableFragment = new TableClassFragment(12);
fragment_trans = getFragmentManager().beginTransaction();
fragment_trans.add(R.id.tableFragment, tableFragment);
fragment_trans.commit();
}
static class TableClassFragment extends Fragment {
int table_size;
String[] sequence_letters_in_table = {};
public TableClassFragment(int _size){
table_size = _size;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return null;
}
}
当我改变方向时 - 得到错误
FATAL EXCEPTION: main
Process: bairro.lettersgenerator, PID: 2885
java.lang.RuntimeException: Unable to start activity ComponentInfo{bairro.lettersgenerator/bairro.lettersgenerator.MainActivity}:
android.app.Fragment$InstantiationException: Unable to instantiate fragment bairro.lettersgenerator.MainActivity$TableClassFragment: make sure class name exists, is public,
and has an empty constructor that is public
Caused by: java.lang.InstantiationException: can't instantiate class bairro.lettersgenerator.MainActivity$TableClassFragment;
no empty constructor
请帮忙
答案 0 :(得分:1)
InstantiationException:[...] TableClassFragment; 没有空构造函数
你TableClassFragment
应该有#34;空构造函数":
static class TableClassFragment extends Fragment {
int table_size;
String[] sequence_letters_in_table = {};
public TableClassFragment() {
// ...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return null;
}
}
使用Fragment.setArguments()
将参数传递给它。
答案 1 :(得分:0)
您的片段中需要一个空的构造函数,这就是您收到此错误的原因。
像:
public TableClassFragment(){
}
答案 2 :(得分:0)
将table_size传递给TableClassFragment作为Bundle
TableClassFragment tableFragment = new TableClassFragment();
Bundle bunble =new Bundle();
bunble.putInt(TableClassFragment.ARG_TABLESIZE , 12);
tableFragment setArguments(bundle);
更改TableClassFragment
public class FaceScanExampleFragment extends Fragment{
final static String ARG_TABLESIZE = "table_size";
int table_size;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
......
if (savedInstanceState != null) {
table_size = savedInstanceState.getInt(ARG_TABLESIZE);
}
......
}
@Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
// Set article based on argument passed in
updateArticleView(args.getInt(ARG_TABLESIZE));
} else
}
}
public void updateArticleView(int position) {
table_size = position;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current article selection in case we need to recreate the fragment
outState.putInt(ARG_TABLESIZE, table_size);
}
答案 3 :(得分:0)
试试这个:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
Toast.makeText(getActivity(), "FirstFragment.onCreateView()",
Toast.LENGTH_LONG).show();
return rootView;
}
}