Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 位置更改时出现空指针异常_Android_Nullpointerexception_Location - Fatal编程技术网

Android 位置更改时出现空指针异常

Android 位置更改时出现空指针异常,android,nullpointerexception,location,Android,Nullpointerexception,Location,我研究了这个空指针异常。在谷歌搜索之后,我发现大多数都是由于对UI元素的空引用造成的。但是我的没有。我就是找不到空指针的原因。我直接在运行2.3.7的android手机上进行了测试。是的,我在清单文件中添加了权限 package com.GodTM.projectshoppers; import android.location.Criteria; import android.location.Location; import android.location

我研究了这个空指针异常。在谷歌搜索之后,我发现大多数都是由于对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;
    }

}

currLoc
从未初始化。由于位置和currLoc都是
位置
您可以更改

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


currLoc
从未初始化。由于位置和currLoc都是
位置
您可以更改

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


您没有初始化
passLoc
。您可以调用
getLastKnownLocation
,而不保存位置本身。

您没有初始化
passLoc
。然后调用
getLastKnownLocation
,而不保存位置本身