我有一个从一个片段传递到另一个片段的 int/string(s)。对于我编码的每一种方式,我都会在第 67 行得到一个 NPE(参见下面的 67)。我不知道我是否以正确的方式进行操作,所以请查看我的方法并在您的答案中包含一些代码。
谢谢。
什么通过了论点:
. . .
int rawRes = R.raw.regulatory_list;//<--THIS IS THE RES I NEED TO PASS
args.putInt("KEY_RAW_RES", rawRes);
boolean isRawRes = true;
args.putBoolean("KEY_IS_RAW_RES", isRawRes);
ListViewFragment lvf = new ListViewFragment();
lcFT.replace(R.id.discriptionListContainer, lvf).commit();
lvf.setArguments(args);
. . .
ListViewFragment 的 onActivityCreated:
. . .
private static final String KEY_URL = "KEY_URL";
private static final String KEY_IS_RAW_RES = "KEY_IS_RAW_RES";
private static final String KEY_RAW_RES = "KEY_RAW_RES";
. . .
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get the string to query from last Fragment and pass it to this
// Fragment
Bundle args = this.getArguments();
String url = args.getString(KEY_URL);
boolean rawRes = args.getBoolean(KEY_IS_RAW_RES);
int fileName = (Integer) null;//<--THIS IS LINE 67!!
fileName = args.getInt(KEY_RAW_RES);
this.runJsonFile(url, jsonSrc, fileName);
}
private void runJsonFile(String url, boolean rawRes, int fileName) {
if (rawRes == true) {
getFromRawRes(fileName);
} else {
getFromURL(url);
}
}
private void getFromRawRes(int fileName) {
// InputStream file = getResources().openRawResource(R.raw.regulatory_list);
InputStream file = getResources().openRawResource(fileName);
JSONParser jParser = new JSONParser();
JSONArray json = jParser.getJSONFromFile(file);
callback(json);
}
编辑:
根据 Raghav Sood 的回答,我将第 67 - 68 行更改为:
// int fileName = (Integer) null;
int fileName = args.getInt(KEY_RAW_RES);
这让课程通过了 NPE - Thnx!