gpt4 book ai didi

android - 让表在 fragment 中,错误是在空对象引用上

转载 作者:行者123 更新时间:2023-11-30 01:02:27 25 4
gpt4 key购买 nike

代码如下:

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 的开源版本。我真的很期待你的帮助。

最佳答案

TableFixHeaders tableFixHeaders = (TableFixHeaders)getActivity().findViewById(R.id.table);

这一行有一些问题,所以 tableFixHeaders 实际上是空的。
您应该先膨胀 View ,然后从膨胀的 View 中找到 subview 。

在您的 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;

关于android - 让表在 fragment 中,错误是在空对象引用上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39245907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com