Listview ArrayAdapter空指针异常

时间:2016-12-18 01:24:19

标签: android listview nullpointerexception android-arrayadapter

我正在尝试获取onMapLongClick(MAPS活动)方法的地址,并将地址保存到列表视图中。列表视图的活动不同。

我收到了nullpointerexception。请参阅下面的完整描述。

我已经回顾了一些来自Stackoverflow的帖子,并对我的数组适配器进行了更改,但空指针异常仍然存在

 E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.android.wesport, PID: 3082
              java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference
                  at com.example.android.wesport.MapsActivity.onMapLongClick(MapsActivity.java:214)
                  at com.google.android.gms.maps.GoogleMap$14.onMapLongClick(Unknown Source)
                  at com.google.android.gms.maps.internal.zzn$zza.onTransact(Unknown Source)
                  at android.os.Binder.transact(Binder.java:499)
                  at ya.a(:com.google.android.gms.DynamiteModulesB:93)
                  at maps.D.p$2.a(Unknown Source)
                  at maps.V.u.f(Unknown Source)
                  at maps.V.P.onLongPress(Unknown Source)
                  at maps.z.e$b.onLongPress(Unknown Source)
                  at maps.z.c.c(Unknown Source)
                  at maps.z.c$a.handleMessage(Unknown Source)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

LongclickMethod:

/* Get address for new places when user long click's on the map) and show the address*/
@Override
public void onMapLongClick(LatLng latLng) {
    Geocoder geocoder=new Geocoder(getApplicationContext(), Locale.getDefault());
    String address="";
    try {
        List<Address> listAddresses=geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
        if (listAddresses!=null && listAddresses.size()>0){
            if (listAddresses.get(0).getThoroughfare()!=null){
                if (listAddresses.get(0).getSubThoroughfare()!=null) {
                    address += listAddresses.get(0).getSubThoroughfare() + " ";
                }
                address += listAddresses.get(0).getThoroughfare();
                Log.d("address",address);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (address == " "){
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm yyyyMMdd");
        address=sdf.format(new Date());
    }
    map.addMarker(new MarkerOptions().position(latLng).title(address));
    MyGames.games.add(address);
    MyGames.locations.add(latLng);
    MyGames.arrayAdapter.notifyDataSetChanged();
    Toast.makeText(this,"Games Saved",Toast.LENGTH_SHORT).show();

ListviewActivity:

static ArrayList<String> games =new ArrayList<>();
static ArrayList<LatLng> locations=new ArrayList<>();
static ArrayAdapter arrayAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_games);
    ListView listView=(ListView) findViewById(R.id.listview);
    locations.add(new LatLng(0,0));
    games.add ("My Saved Games...");
    if(arrayAdapter.getCount()!=0 && arrayAdapter!=null){
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,games);
    listView.setAdapter(arrayAdapter);
    }

1 个答案:

答案 0 :(得分:0)

问题在于静态变量。阵列适配器初始化不正确。我删除了静态变量并使用共享首选项来存储我的数组并在另一个活动中检索它