PID:15856 java.lang.NullPointerException:

时间:2019-05-21 00:19:43

标签: java android android-studio

当我运行旋转器时,我看到此错误。 PID:15856     java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'。这些是我的代码。

我在互联网上搜索此错误,但无法解决问题。

公共类outdoorActivities扩展了AppCompatActivity {

LocationManager locationManager;
LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        TextView textView = (TextView)findViewById(R.id.milesTextview);




        Location loc3 = new Location("Hidden Valley");
        loc3.setLatitude(50.6377016);
        loc3.setLongitude(-4.4379017);

        Location loc4 = new Location("Newquay Zoo");
        loc4.setLatitude(50.411383);
        loc4.setLongitude(-5.0716177);

        Location loc5 = new Location("Seal Sanctuary");
        loc5.setLatitude(50.0951784);
        loc5.setLongitude(-5.2080155);



        float distanceInMeters3 = location.distanceTo(loc3);
        textView.setText(String.valueOf(distanceInMeters3)+"  miles");

        float distanceInMeters4 = location.distanceTo(loc4);
        textView.setText(String.valueOf(distanceInMeters4)+"  miles");

        float distanceInMeters5 = location.distanceTo(loc5);
        textView.setText(String.valueOf(distanceInMeters5)+"  miles");


        activity1.setDistancemile(distanceInMeters3);
        activity2.setDistancemile(distanceInMeters4);
        activity3.setDistancemile(distanceInMeters5);




    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};





CornwallActivity activity1 = new CornwallActivity();
CornwallActivity activity2 = new CornwallActivity();
CornwallActivity activity3 = new CornwallActivity();


CornwallActivity[] activities = new CornwallActivity[3];





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_outdoor_activities);
    Intent intent = getIntent();

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
    } else {
        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,0,0,locationListener);

    }


    ListView listView = (ListView)findViewById(R.id.ListView);
    CustomAdapter customAdapter = new CustomAdapter();


    activity1.setName("HIDDEN VALLEY");
    activity2.setName("NEWQUAY ZOO");
    activity3.setName("SEAL SANCTUARY");
    activity1.setDescription("hidden valley description will be here hidden valley description will be here ");
    activity2.setDescription("newquay zoo description will be here newquay zoo description will be here ");
    activity3.setDescription("seal sanctuary description will be here seal sanctuary description will be here ");

    activity1.setimagesoutdoor(R.drawable.hiddenvalley);
    activity2.setimagesoutdoor(R.drawable.newquayzoo);
    activity3.setimagesoutdoor(R.drawable.sealsanctuary);


    activities[0] = activity1;
    activities[1] = activity2;
    activities[2] = activity3;

    listView.setAdapter(customAdapter);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    {
        if (grantResults.length>0){
            if (requestCode==1){
                if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) ==PackageManager.PERMISSION_GRANTED){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,50,locationListener);
                }

            }

        }

    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

class CustomAdapter extends BaseAdapter{


    @Override
    public int getCount() {
        return activities.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup parent) {
        view = getLayoutInflater().inflate(R.layout.customlayout,null);
        ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
        TextView textView_name = (TextView)view.findViewById(R.id.textView_name);
        TextView textView_description = (TextView)view.findViewById(R.id.textView_description);
        TextView textView_distanceinmeters = (TextView)view.findViewById(R.id.textView_distanceinmeters);

        imageView.setImageResource(activities[i].getimagesoutdoor());
        textView_name.setText(activities[i].getName());
        textView_distanceinmeters.setText(String.valueOf(activities[i].getDistancemile()));
        textView_description.setText(activities[i].getDescription());

        return view;
    }

0 个答案:

没有答案