简体   繁体   English

设置自定义适配器时发生NullPointerException

[英]NullPointerException when setting custom adapter

I'm trying to use a custom adapter with ListView on my main activity and it crashes. 我正在尝试在我的主要活动上使用带有ListView的自定义适配器,它会崩溃。 I've looked everywhere for an answer please help! 我到处都在寻找答案,请帮忙!

My main activity/ ListActivity: 我的主要活动/ 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 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. 我检查了XML中的所有ID,它们都存在,等等……我怀疑我的自定义适配器是问题所在,但我不明白我在做什么错。 Struggling student here with limited android experience, please help! 在Android经验有限的地区奋斗的学生,请帮忙!

Firstly, you should reuse views if they exist instead of creating new ones when not needed, it saves ram and improves performance. 首先,您应该重用视图(如果存在),而不是在不需要时创建新视图,这样可以节省ram并提高性能。 Edit your getView() method like the following: 如下编辑您的getView()方法:

@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. 正如Gabe所说,在解决您的问题时,您的应用程序类中的getArray()方法将调整为null。 Check that out. 检查出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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