I have a list that needs to be populated in a GridView but the application crashes after returning a NullPointer exception. Everything looks correct though. Have a look and help me out if you can please :)
XML GridView Layout (Name of XML - comments_frag.xml)
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="180dp"
android:layout_marginLeft="@dimen/feed_item_margin"
android:layout_marginRight="@dimen/feed_item_margin"
android:layout_marginTop="@dimen/feed_item_margin"
android:background="@drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:paddingBottom="@dimen/feed_item_padding_top_bottom"
android:paddingTop="@dimen/feed_item_padding_top_bottom" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/commentinglist"
android:layout_width="295dp"
android:layout_height="142dp"
android:columnWidth="100dp"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:gravity="center"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Single GridView Item Layout (Name of XML - comment_single.xml)
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<!--android:background="@drawable/image_bg" -->
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="John Doe"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/user_comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/user_name"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="This is a sample comment! :)" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/user_name"
android:gravity="right"
android:text="12/20/14"
android:layout_marginRight="5dip"
android:textSize="10dip"
android:textColor="#10bcc9"
android:textStyle="bold"/>
</RelativeLayout>
GridView Adaptor
public class CommentsAdapter extends ArrayAdapter<Feedback>{
ArrayList<Feedback> comments;
Context ctx;
int resource;
public CommentsAdapter(Context context, int resource,ArrayList<Feedback> commentsList) {
super(context, resource, commentsList);
this.comments = commentsList;
this.ctx = context;
this.resource = resource;
}
@Override
public int getCount() {
if(comments.size() == 0){
return 0;
}else{
return comments.size();
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View child = convertView;
RecordHolder holder;
LayoutInflater inflater = ((Activity) ctx).getLayoutInflater();
if (child == null) {
child = inflater.inflate(R.layout.comment_single, parent, false);
holder = new RecordHolder();
holder.user = (TextView) child.findViewById(R.id.user_name);
holder.comment = (TextView) child.findViewById(R.id.user_comment);
child.setTag(holder);
}else{
holder = (RecordHolder) child.getTag();
}
final Feedback user = comments.get(position);
holder.comment.setText(user.getComment());
holder.user.setText(user.getCommenter());
return child;
}
static class RecordHolder {
public TextView user;
public TextView comment;
}
}
Activity from which the call is made to populate (Name of Activity - ProductSingle_Activity.java):
Feedback f = new Feedback();
f.Comment = "NICE!";
f.Commenter = "Dinuka Jayasuriya";
f.Date = "2014/12/20";
Feedback f2 = new Feedback();
f2.Comment = "SUPER!";
f2.Commenter = "John Doe";
f2.Date = "2014/12/20";
ArrayList<Feedback> arr = new ArrayList<Feedback>();
arr.add(f);
arr.add(f2);
GridView list = (GridView)findViewById(R.id.commentinglist);
CommentsAdapter bA = new CommentsAdapter(this, R.layout.comments_frag, arr);
list.setAdapter(bA);
LOGCAT (Sorry if it's messy. How do you format it on Stackoverflow?):
12-20 19:48:17.491: E/AndroidRuntime(11875): FATAL EXCEPTION: main
12-20 19:48:17.491: E/AndroidRuntime(11875): Process: com.jezzaappgoogle, PID: 11875
12-20 19:48:17.491: E/AndroidRuntime(11875): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jezzaappgoogle/activity.classes.ProductSingle_Activity}: java.lang.NullPointerException
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.ActivityThread.access$900(ActivityThread.java:161)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.os.Looper.loop(Looper.java:157)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.ActivityThread.main(ActivityThread.java:5356)
12-20 19:48:17.491: E/AndroidRuntime(11875): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 19:48:17.491: E/AndroidRuntime(11875): at java.lang.reflect.Method.invoke(Method.java:515)
12-20 19:48:17.491: E/AndroidRuntime(11875): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-20 19:48:17.491: E/AndroidRuntime(11875): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-20 19:48:17.491: E/AndroidRuntime(11875): at dalvik.system.NativeStart.main(Native Method)
12-20 19:48:17.491: E/AndroidRuntime(11875): Caused by: java.lang.NullPointerException
12-20 19:48:17.491: E/AndroidRuntime(11875): at activity.classes.ProductSingle_Activity.onCreate(ProductSingle_Activity.java:76)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.Activity.performCreate(Activity.java:5426)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-20 19:48:17.491: E/AndroidRuntime(11875): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
No comments:
Post a Comment