这是代码:
public class Persion1_fragment extends Fragment{
Context mContext;
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
this.mContext = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TableFixHeaders tableFixHeaders = (TableFixHeaders)getActivity().findViewById(R.id.table);
String [][] str=new String[][] {
{
"Header 1",
"Header 2",
"Header 3",
"Header 4",
"Header 5",
"Header 6" },
};
MatrixTableAdapter<String> matrixTableAdapter = new MatrixTableAdapter<String>(this.mContext,str);
tableFixHeaders.setAdapter(matrixTableAdapter);
return inflater.inflate(R.layout.persion3, container, false);
}
}
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.inqbarna.tablefixheaders.TableFixHeaders.setAdapter(com.inqbarna.tablefixheaders.adapters.TableAdapter)' on a null object reference
我正在使用TableFixHeaders的开源版本。 我非常期待帮助。
答案 0 :(得分:1)
TableFixHeaders tableFixHeaders = (TableFixHeaders)getActivity().findViewById(R.id.table);
这一行存在一些问题,因此tableFixHeaders实际上是null。
您应首先使视图膨胀,然后从膨胀的视图中查找子视图。
在onCreateView()中尝试此代码。
View view = inflater.inflate(R.layout.persion3, container, false);
TableFixHeaders tableFixHeaders = (TableFixHeaders)view.findViewById(R.id.table);
String [][] str=new String[][] {
{
"Header 1",
"Header 2",
"Header 3",
"Header 4",
"Header 5",
"Header 6" },
};
MatrixTableAdapter<String> matrixTableAdapter = new MatrixTableAdapter<String>(this.mContext,str);
tableFixHeaders.setAdapter(matrixTableAdapter);
return view;