• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Custom List: Project crashes, can't understand why!

I am a new android learner. I was making a project to explore custom list. The goal of the project is to show 4 TextView in every list. Two TextView are fixed and two will be shown from an object holding two string(A Movie name and its rating). But the program is crashing. I can't detect any error! Here is my code:

MainActivity Code:

Code:
package com.sfinix.customlist;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

   /* data to show in each list */
   Movie movies[] = new Movie[]{
     new Movie("The Matrix", "9.0"),
     new Movie("Inception", "8.0"),
     new Movie("Interstellar", "8.5"),
     new Movie("The Theory of Everything", "8.5")
   };
  
   /* custom ArrayAdapter object*/
   MovieListAdapter adapter;
  
   /* ListView object */
   ListView lv;
  
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    
     /* initialization */
     adapter = new MovieListAdapter(this, R.layout.movie_list_items, movies);
     lv = (ListView) findViewById(R.id.listView);
    
     /* set Adapter */
     lv.setAdapter(adapter);
   }

}

main.xml Code:


Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <ListView
  android:id="@+id/listView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
  </ListView>

</LinearLayout>

My Object Class:

Code:
package com.sfinix.customlist;

public class Movie {
   String name, rating;

   public Movie(String name, String rating) {
     this.name = name;
     this.rating = rating;
   }

   public String getName() {
     return name;
   }

   public void setName(String name) {
     this.name = name;
   }

   public String getRating() {
     return rating;
   }

   public void setRating(String rating) {
     this.rating = rating;
   }
  
}

XML Layout for each list:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="3"
  android:text="Name:" />

  <TextView
  android:id="@+id/tvName"
  android:layout_weight="1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Movie Name" />
  </LinearLayout>

  <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >
  
  <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="3"
  android:text="Rating:" />

  <TextView
  android:id="@+id/tvRating"
  android:layout_weight="1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Rating" />
  
  </LinearLayout>

</LinearLayout>

Custom Adapter:

Code:
package com.sfinix.customlist;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/* this is the custom array adapter for custom list */

public class MovieListAdapter extends ArrayAdapter<Movie>{
   Context context;
   int layoutResourceId;
   Movie[] movie = null;
  
   /* costructor for the array adapter. Takes context(from which activity it was called), layout resource id(layout which represents what items will be shown in each list), and source of the data which will be shown */
   public MovieListAdapter(Context context, int layoutResourceId, Movie[] movie){
     super(context, layoutResourceId, movie);
     this.context = context;
     this.layoutResourceId = layoutResourceId;
     this.movie = movie;
   }
  
   /* override getView(int position, View convertView, ViewGroup parent) to generate view. It returns generated view */
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
     /* convertView is for performance issue. If a View is already created it is taken from convertView */
     View row = null;
     MovieHolder holder = null;
    
     /* if view is not generated than generate view */
     if(convertView == null){
       /* take a layout inflater object. Explicitly type cast Activity */
       LayoutInflater inflater = ((Activity)context).getLayoutInflater();
       /* infale View */
       row = inflater.inflate(layoutResourceId, null);
      
       /* initialize the elements */
       holder = new MovieHolder();
       holder.tvName = (TextView) row.findViewById(R.id.tvName);
       holder.tvRating = (TextView) row.findViewById(R.id.tvRating);
     }
     else{
       row = convertView;
     }
    
     Movie newmovie = movie[position];
     holder.tvName.setText(newmovie.getName());
     holder.tvRating.setText(newmovie.getRating());
    
     return row;
   }
  
   static class MovieHolder{
     TextView tvName, tvRating;
   }
}

Log Cat:

Code:
02-04 16:51:14.492: D/dalvikvm(1329): GC_FOR_ALLOC freed 44K, 7% free 2555K/2720K, paused 47ms, total 50ms
02-04 16:51:14.502: I/dalvikvm-heap(1329): Grow heap (frag case) to 3.217MB for 635812-byte allocation
02-04 16:51:14.562: D/dalvikvm(1329): GC_FOR_ALLOC freed 2K, 6% free 3174K/3344K, paused 59ms, total 59ms
02-04 16:51:14.632: D/dalvikvm(1329): GC_CONCURRENT freed <1K, 5% free 3184K/3344K, paused 9ms+12ms, total 72ms
02-04 16:51:14.772: D/AndroidRuntime(1329): Shutting down VM
02-04 16:51:14.772: W/dalvikvm(1329): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
02-04 16:51:14.812: E/AndroidRuntime(1329): FATAL EXCEPTION: main
02-04 16:51:14.812: E/AndroidRuntime(1329): java.lang.NullPointerException
02-04 16:51:14.812: E/AndroidRuntime(1329):    at com.sfinix.customlist.MovieListAdapter.getView(MovieListAdapter.java:50)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.AbsListView.obtainView(AbsListView.java:2143)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.ListView.onMeasure(ListView.java:1158)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.View.measure(View.java:15518)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.View.measure(View.java:15518)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.View.measure(View.java:15518)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.View.measure(View.java:15518)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.View.measure(View.java:15518)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.Choreographer.doFrame(Choreographer.java:532)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.os.Handler.handleCallback(Handler.java:725)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.os.Looper.loop(Looper.java:137)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at android.app.ActivityThread.main(ActivityThread.java:5041)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at java.lang.reflect.Method.invoke(Method.java:511)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-04 16:51:14.812: E/AndroidRuntime(1329):    at dalvik.system.NativeStart.main(Native Method)
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones