我正在尝试将数据加载到单独片段上的表中。我将数据保存在文本文件中,然后将其转换为数组,可以正常工作,但是在尝试使其在启动时可以正常工作。我认为我的问题是当我调用loadData方法时,但我可能错了。我在MainActivity中称呼它。
我得到的错误是:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TableLayout.addView(android.view.View)'
这是我加载数据的方法:
public void loadData() {
//Accounts
File path = Environment.getExternalStorageDirectory();
File myExternalFile = new File(path.getAbsolutePath() + "/App_Folder/", "accounts.txt");
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
accounts.add(strLine);
}
br.close();
in.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < accounts.size(); i++) {
String[] splitArray = accounts.get(i).split("//");
String accountNameText = splitArray[0];
String amountSavedText = splitArray[1];
String goalText = splitArray[2];
String percentText = splitArray[3];
// get a reference for the TableLayout
TableLayout table = (TableLayout) findViewById(R.id.accountTable);
// create a new TableRow
TableRow row = new TableRow(this);
TextView accountName = new TextView(this);
accountName.setText(accountNameText);
TextView amountSaved = new TextView(this);
amountSaved.setText(amountSavedText);
TextView accountGoal = new TextView(this);
accountGoal.setText(goalText);
TextView accountPercent = new TextView(this);
accountPercent.setText(percentText);
row.addView(accountName);
row.addView(amountSaved);
row.addView(accountGoal);
row.addView(accountPercent);
table.addView(row);
}
}
答案 0 :(得分:0)
您可以在Fragment内以这种方式进行操作(此代码仅用于示例):
public class MyFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view= inflater.inflate(R.layout.myfragment_view, container, false);
//call loaddata
loadData();
return view;
}
public void loadData() {
File path = Environment.getExternalStorageDirectory();
File myExternalFile = new File(path.getAbsolutePath() + "/App_Folder/", "accounts.txt");
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
accounts.add(strLine);
}
br.close();
in.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < accounts.size(); i++) {
String[] splitArray = accounts.get(i).split("//");
String accountNameText = splitArray[0];
String amountSavedText = splitArray[1];
String goalText = splitArray[2];
String percentText = splitArray[3];
// get a reference for the TableLayout,
TableLayout table = (TableLayout)view.findViewById(R.id.accountTable);
// create a new TableRow
TableRow row = new TableRow(getContext());
TextView accountName = new TextView(getContext());
accountName.setText(accountNameText);
TextView amountSaved = new TextView(getContext());
amountSaved.setText(amountSavedText);
TextView accountGoal = new TextView(getContext());
accountGoal.setText(goalText);
TextView accountPercent = new TextView(getContext());
accountPercent.setText(percentText);
row.addView(accountName);
row.addView(amountSaved);
row.addView(accountGoal);
row.addView(accountPercent);
table.addView(row);
}}}