0

この Nullpointer Exception について調査しました。グーグルで調べたところ、それらのほとんどは UI 要素への null 参照が原因であることがわかりました。しかし、私のものには何もありません。nullポインターの理由がわかりません。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