0

我研究了这个 Nullpointer Exception。谷歌搜索后,我发现其中大部分是由于对 UI 元素的空引用。但我的没有。我只是无法得到空指针的原因。我直接在运行 2.3.7 的 android 手机上对其进行了测试。是的,我在清单文件中添加了权限。

    package com.GodTM.projectshoppers;

    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.location.LocationProvider;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class HomeActivity extends Activity {

    private ImageButton get;
    private ImageView quit,settings;
    private LocationManager locMan;
    public LocationListener LocLis;
    public Location currLoc;
    private double[] passloc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        get=(ImageButton)findViewById(R.id.homeGetButton);
        settings = (ImageView)findViewById(R.id.homeSettingsButton);
        quit = (ImageView)findViewById(R.id.homeExitButton);

        Criteria crit=new Criteria();
        locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        LocLis = new LocationListener() {

            @Override
            public void onStatusChanged(String provider, int status, Bundle     extras) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                if(location != null){
                    currLoc.setLatitude(location.getLatitude());
                    currLoc.setLongitude(location.getLongitude());
                }
            }
        };

        //Criteria crit  = new Criteria();

        if(currLoc !=null){
            passloc[0] = currLoc.getLatitude();
            passloc[1] = currLoc.getLongitude();
            Toast.makeText(getApplicationContext(), "Lat:" + currLoc.getLatitude() + "\nLong:" + currLoc.getLongitude(), Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(getApplicationContext(), "Not Available", Toast.LENGTH_SHORT).show();
        }


        get.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(currLoc!=null){
                    Intent i = new Intent(HomeActivity.this, Screen2.class);
                    i.putExtra("Loc", passloc);
                    startActivity(i);
                }
                else{
                    Toast.makeText(getApplicationContext(), "Location Not Available", Toast.LENGTH_SHORT).show();
                }
            }
        });

        settings.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//              Intent j = new Intent(this, screen4.class);
//              startActivity(j);
            }
        });

        quit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();               
            }
        });
    }

    @Override
    public void onResume(){
        super.onResume();
        locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER , 0, 0, LocLis);
    }

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

}
4

2 回答 2

3

currLoc永远不会被初始化。由于位置和currLoc两者都Location可以更改

if(location != null){
     currLoc.setLatitude(location.getLatitude());
     currLoc.setLongitude(location.getLongitude());
}

currLoc = location;
于 2013-07-07T18:35:46.970 回答
0

你没有初始化passLoc。而且您在getLastKnownLocation不保存位置本身的情况下调用。

于 2013-07-07T18:36:42.690 回答