【问题标题】:List Activity not starting列表活动未开始
【发布时间】:2017-10-12 10:30:01
【问题描述】:
private ListView listView;
private List<String> userNames,desc,lat,lon,image, placeNames;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_places_list);
    setTitle("Places List");

    listView=(ListView) findViewById(R.id.placesListView);
    userNames=new ArrayList<String>();
    placeNames = new ArrayList<String>();
    desc=new ArrayList();image=new ArrayList();
    lat=new ArrayList();lon=new ArrayList();



    userNames=getIntent().getStringArrayListExtra("userNames");
    placeNames=getIntent().getStringArrayListExtra("placeNames");
    desc=getIntent().getStringArrayListExtra("desc");
    image=getIntent().getStringArrayListExtra("image");
    lat=getIntent().getStringArrayListExtra("lat");
    lon=getIntent().getStringArrayListExtra("lon");

    final ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,userNames);
    listView.setAdapter(arrayAdapter);

java.lang.NullPointerException:尝试在空对象引用上调用接口方法“int java.util.List.size()”在空对象引用上的java.util.List.size() 如何避免异常?

【问题讨论】:

  • List 'userNames' 不知何故为空,检查一下!!

标签: android listview android-activity arraylist


【解决方案1】:

我认为当您尝试在适配器中设置 userNames 时,它会发生只是因为您将 userNames 列表设置为 null 所以首先检查然后设置如下

if(userNames!=null){
final ArrayAdapter arrayAdapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,userNames);
    listView.setAdapter(arrayAdapter);
}

【讨论】:

    【解决方案2】:
    Now the app is not crashing , but list isn't displaying. 
    I am able to display values in invoking activity.    
        `enter code here`
    if(userNames.size()>0)
    {
        Log.i("OOOOOOO",userNames.get(0)+"'"+userNames.get(1));
    }        
    Intent intent=new Intent(ViewActivity.this,PlacesListActivity.class);        intent.putStringArrayListExtra("userNames",userNames);intent.putStringArrayListExtra("placeNames",placeNames);
                    intent.putStringArrayListExtra("desc",desc);intent.putStringArrayListExtra("image",image);
                    intent.putStringArrayListExtra("lat",lat);intent.putStringArrayListExtra("lon",lon);
                    startActivity(new Intent(ViewActivity.this,PlacesListActivity.class));
    

    【讨论】:

    • 如果我包含相同的日志语句 ................................... ..................................................... .............................if(placeNames.size()>0){{ Log.i("OOOOOO",placeNames. get(0)+"'"+placeNames.get(1));}.............在List活动中,异常重复
    • startActivity(new Intent(ViewActivity.this,PlacesListActivity.class));改为startActivity(intent);
    • 调试并确保所有arrayList在ViewActivity类中都有数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多