in my application i have section as view comment like with GooglyPlay. this comments count is unknow and i must be add layout from external layout into current layout, i get data from json and i want to create this section with json count. for example i like to have :
my json array is:
[
{"comment":"test1"},
{"comment":"test2"},
{"comment":"test3"},
{"comment":"test4"}
]
now in this code i must be add external layout and add it into current layout:
<ViewStub
android:id="@+id/layout_stub_comments"
android:inflatedId="@+id/comments_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
attach layout from code and retrive data from json:
private void parseCommentsContent(String jsonStr) {
try {
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++) {
String comment = jsonArray.getJSONObject(i).getString(SV.FIELD_COMMENT);
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub_comments);
stub.setLayoutResource(R.layout.layout_comments);
View inflated = stub.inflate();
TextView comment_text = (TextView) inflated.findViewById(R.id.tv_comment_text);
comment_text.setText(comment);
}
} catch (JSONException e) {
e.printStackTrace();
UC.debugLog(traceElements, G.DEBUG, SV.Messages[3]);
}
}
in this phase i get NullPointerException for this line:
stub.setLayoutResource(R.layout.layout_comments);
layout_comments layout designed into layout folder as:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="50dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/tv_comment_text"/>
</LinearLayout>
</LinearLayout>
why i get i get NullPointerException? thanks
Aucun commentaire:
Enregistrer un commentaire