简体   繁体   中英

NullPointerException when setting custom adapter

I'm trying to use a custom adapter with ListView on my main activity and it crashes. I've looked everywhere for an answer please help!

My main activity/ ListActivity:

public class ListRestaurantsMain extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_restaurants_main);

    //access myApp
    MyApplication myApp = MyApplication.getInstance();
    ArrayList <RestaurantObj> arr = new ArrayList <RestaurantObj>();

    arr = myApp.getArray();

    ListView lv = (ListView) findViewById(android.R.id.list);
    RatingAdapter adapter = new RatingAdapter(getApplicationContext(), arr);
    lv.setAdapter(adapter);


}
  @Override
  protected void onListItemClick(ListView lv, View v, int position, long id) {
      super.onListItemClick(lv, v, position, id); 

      Log.v("ClickListener", "Item at pos: " + position + " clicked.");
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.list_restaurants_main, menu);
    return true;
}



}

My custom Adapter:

class RatingAdapter extends ArrayAdapter<RestaurantObj> {

public RatingAdapter(Context context, ArrayList<RestaurantObj> objects) {
    super(context, R.layout.list_row, R.id.title, objects);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;

    //get info from Restaurant Object
    RestaurantObj restObj = getItem(position);

    // create new inflater
        row = LayoutInflater.from(getContext()).inflate(R.layout.list_row, null);


    //display name of restaurant
    TextView restName = (TextView)row.findViewById(R.id.title);
    restName.setText(restObj.getName());

    //display rating
    RatingBar rb = (RatingBar) row.findViewById(R.id.ratingbar);
    rb.setRating(3); //grab rating from object

    return row;
}
}

Logcat

03-16 03:04:16.679: E/AndroidRuntime(1865): FATAL EXCEPTION: main
03-16 03:04:16.679: E/AndroidRuntime(1865): Process: com.example.restaurantrater, PID: 1865
03-16 03:04:16.679: E/AndroidRuntime(1865): java.lang.RuntimeException: Unable to start activity        ComponentInfo{com.example.restaurantrater/com.example.restaurantrater.ListRestaurantsMain}: java.lang.NullPointerException
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.os.Looper.loop(Looper.java:136)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at java.lang.reflect.Method.invoke(Method.java:515)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at dalvik.system.NativeStart.main(Native Method)
03-16 03:04:16.679: E/AndroidRuntime(1865): Caused by: java.lang.NullPointerException
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.widget.ListView.setAdapter(ListView.java:480)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at com.example.restaurantrater.ListRestaurantsMain.onCreate(ListRestaurantsMain.java:32)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.Activity.performCreate(Activity.java:5231)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-16 03:04:16.679: E/AndroidRuntime(1865):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-16 03:04:16.679: E/AndroidRuntime(1865):     ... 11 more

I checked all the ids in the XMLs and they all exist and whatnot... I suspect my custom adapter is the problem but I don't understand what I'm doing wrong. Struggling student here with limited android experience, please help!

Firstly, you should reuse views if they exist instead of creating new ones when not needed, it saves ram and improves performance. Edit your getView() method like the following:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_row, null);
    }

    RestaurantObj restObj = getItem(position);

    TextView restName = (TextView)row.findViewById(R.id.title);
    restName.setText(restObj.getName());

    RatingBar rb = (RatingBar) row.findViewById(R.id.ratingbar);
    rb.setRating(3);

    return convertView;
}

Onto addressing your issue, as also Gabe said, the getArray() method from your application class is retuning null. Check that out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM