下面的代碼報出NullPointerException
異常,因為ArrayList沒有正確轉換成Array。數組需要傳遞到另一個Activity中,我在傳遞常規String[]時成功了,但是轉換ArrayList到數組時報錯。
應該怎麼轉換才正確?
String[] tmpHtml = new String[(siteElements.size())];
tmpHtml = (String[]) siteElements.toArray();
Intent returnResult = new Intent();
returnResult.putExtra("elements", tmpHtml);
setResult(RESULT_OK, returnResult);
finish();
String[] tmpHtml;
tmpHtml = siteElements.toArray(new String[0]);
Intent returnResult = new Intent();
returnResult.putExtra("elements", tmpHtml);
setResult(RESULT_OK, returnResult);
finish();