【问题标题】:Listview ArrayAdapter null pointer exceptionListview ArrayAdapter 空指针异常
【发布时间】:2016-12-18 01:24:18
【问题描述】:

我正在尝试获取 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)

长按方法:

/* 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();

列表视图活动:

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);
    }

【问题讨论】:

  • 使用arrayAdapter=new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1,games);
  • 你从来没有初始化过static ArrayAdapter arrayAdapter; 并且静态变量通常是不好的,反正
  • 我刚改成arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_‌​1,games),还是报错
  • 好的,但它可能不是完全相同的错误。 Edit您的问题与最新的代码和错误
  • 实际上,删除&amp;&amp; arrayAdapter!=null,因为这似乎是错误的

标签: android listview nullpointerexception android-arrayadapter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多