我有一个登录页面。如果用户成功登录,则应启动新的Activity
,其中包含包含用户私有项目的ListFragment
。我遍历所有项目标题(从我得到的json结果)并将其存储在一个数组中。然后使用详细信息(项目标题)对ListFragment进行充气。只要用户单击提交,就会调用ProjectListActivity.java
。但是我收到了错误:
java.lang.NullPointerException: Attempt to get length of null array
我在这里做错了什么?
private_projects_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/privateProjectsContainer">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.amangrover.finalapp1.ProjectListFragment" />
</FrameLayout>
ProjectListActivity.java:
public class ProjectListActivity extends Activity {
private final static String TAG_TITLE = "title";
private final static String TAG_ANIM_ARRAY_ID = "animation_array";
private final static String TAG_PROJECTS = "projects";
private final static String TAG_ID = "id";
JSONArray projects = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new HttpGetHandler().execute();
setContentView(R.layout.private_project_list);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.privateProjectsContainer, new ProjectListFragment());
ft.commit();
}
private class HttpGetHandler extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String jsonUrl = "some url";
HttpGet httpGet = new HttpGet(jsonUrl);
try {
HttpResponse httpResponse = MainActivity.httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream content = httpEntity.getContent();
String result = convertToString(content);
JSONObject jsonObject = new JSONObject(result);
projects = jsonObject.getJSONArray(TAG_PROJECTS);
ProjectListActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try{
ArrayList<String> projectTitle = new ArrayList<String>();
ArrayList<String> imageId = new ArrayList<String>();
ArrayList<String> playId = new ArrayList<String>();
for(int i=0; i<projects.length(); i++) {
JSONObject p = projects.getJSONObject(i);
String title = p.getString(TAG_TITLE);
String id = p.getString(TAG_ID);
String array_anim_id = p.getString(TAG_ANIM_ARRAY_ID);
String imgId = array_anim_id.substring(0, 4);
projectTitle.add(title);
imageId.add(imgId);
playId.add(id);
}
String[] arrProjectTitle = new String[projectTitle.size()];
String[] arrImageId = new String[imageId.size()];
final String[] pid = new String[playId.size()];
for(int i = 0; i<projectTitle.size(); i++)
arrProjectTitle[i] = projectTitle.get(i);
for(int j = 0; j<imageId.size(); j++)
arrImageId[j] = imageId.get(j);
for(int k = 0; k<playId.size(); k++)
pid[k] = playId.get(k);
/*Bundle b = new Bundle();
b.putStringArray("projectTitle", arrProjectTitle);
b.putStringArray("projectId", pid);
b.putStringArray("imageId", arrImageId);*/
Bundle b = new Bundle();
b.putStringArray("projectTitle", arrProjectTitle);
ProjectListFragment fragment = new ProjectListFragment();
fragment.setArguments(b);
}catch(Exception e) {
}
}
});
}catch(Exception e) {
}
return null;
}
public String convertToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine())!=null) {
result += line;
}
inputStream.close();
return result;
}
}
}
如果用户已登录,我必须对some url
进行GET调用。所以我必须使用AsyncTask
。虽然我检查了调试器,但我使用json url获取数据,但错误在Fragment类的代码中:
ProjectListFragment.java:
public class ProjectListFragment extends ListFragment{
String[] projectTitle;
String[] projectImage;
CustomAdapter adapter;
private List<RowItem> rowItems;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, null, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/*Bundle args = getActivity().getIntent().getExtras();
projectTitle = args.getStringArray("projectTitle");
projectImage = args.getStringArray("imageId");*/
Bundle bundle = this.getArguments();
if(bundle!=null){
projectTitle = bundle.getStringArray("projectTitle");
}
rowItems = new ArrayList<RowItem>();
for(int i=0; i<projectTitle.length; i++) {
RowItem item = new RowItem(projectTitle[i], 0);
rowItems.add(item);
}
adapter = new CustomAdapter(getActivity(), rowItems);
setListAdapter(adapter);
}
}
我在for循环中收到错误。
答案 0 :(得分:0)
Bundle b = new Bundle();
b.putStringArray("projectTitle", arrProjectTitle);
ProjectListFragment fragment = new ProjectListFragment();
fragment.setArguments(b);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.privateProjectsContainer, fragment);
ft.commit();
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/privateProjectsContainer">
</FrameLayout>
最好使用代码执行片段事务。并使用您的片段替换容器布局。在你的情况下,你没有将这些参数传递给新创建的frgament
答案 1 :(得分:0)
jsonObject.getJSONArray(TAG_PROJECTS);
- 返回null。
在此行中:
for(int i=0; i<projects.length(); i++) {
你得到NullPointerException
要解决这个问题,请做下一个:
if(projects != null){
ProjectListActivity.this.runOnUiThread(new Runnable() {/*Code here*/});
}