findViewById in non-Activity class

蓝咒 提交于 2019-12-08 19:20:49

问题


Still being relatively new to this I'm having issues in finding a view in a non-activity class MyLocation which I'm using in my activity class MainActivity. I'm using MyLocation to get longitude and latitude. I want to highlight a textview when either GPS or Network has been used. To do so I need to find the textviews in the non-activity class MyLocation.

Here is how I'm calling it in MainActivity:

public class MainActivity extends ActionBarActivity implements LocationListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            MyLocation myLocation = new MyLocation();
            myLocation.getLocation(this, locationResult);

}

And here what I tried in MyLocation to find the textviews:

public class MyLocation {

LocationManager lm;
LocationResult locationResult;
private Context context;
TextView tvnetwork, tvgps;
private int defaultTextColor;

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {

        locationResult.gotLocation(location);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.main, null);

        tvnetwork = (TextView) v.findViewById(R.id.tvnetwork);
        tvgps = (TextView) v.findViewById(R.id.tvgps);
        defaultTextColor = tvgps.getTextColors().getDefaultColor();

        tvnetwork.setTextColor(context.getResources().getColor(
                R.color.green));
        tvgps.setTextColor(defaultTextColor);

        lm.removeUpdates(this);
        lm.removeUpdates(locationListenerGps);
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

But the views are not found. I already get a NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);. What am I doing wrong?


回答1:


get a NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);. What am I doing wrong?

Because context is null in MyLocation class. use MyLocation class constructor to pass MainActivity context in MyLocation to access system services as:

Activity activity;
public MyLocation(Context context,Activity activity){
this.context=context;
this.activity=activity;
}

and in MainActivity create MyLocation class object by passing MainActivity context as:

MyLocation myLocation = new MyLocation(MainActivity.this,this);

Now use context to access system services in MyLocation class

EDIT: Instead of inflating main layout again in onLocationChanged use Activity context to access view from Activity Layout as:

 public void onLocationChanged(Location location) {

       ....
        tvnetwork = (TextView) activity.findViewById(R.id.tvnetwork);
        tvgps = (TextView) activity.findViewById(R.id.tvgps);
        defaultTextColor = tvgps.getTextColors().getDefaultColor();

        tvnetwork.setTextColor(context.getResources().getColor(
                R.color.green));
        tvgps.setTextColor(defaultTextColor);

       ....
    }



回答2:


Why you want to do it in separate class as your are implementing "LocationListener" in Activity itself. And if you want to do in separate class. You can notify the activity by setting custom listners like this

public class MainActivity extends ActionBarActivity implements LocationListener,CustomListner {

        TextView tvnetwork, tvgps;
        private int defaultTextColor;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tvnetwork = (TextView) v.findViewById(R.id.tvnetwork);
            tvgps = (TextView) v.findViewById(R.id.tvgps);
            defaultTextColor = tvgps.getTextColors().getDefaultColor();
            MyLocation myLocation = new MyLocation(this); // pass listner in constructor
            myLocation.getLocation(this, locationResult);

        }
        @Override
        public void highlight(){
              // update your view here.
              tvnetwork.setTextColor(context.getResources().getColor(
                        R.color.green));
                tvgps.setTextColor(defaultTextColor);
        }



        public class MyLocation {

        LocationManager lm;
        LocationResult locationResult;
        CustomListner listner;

        MyLocation(CustomListner listner){
        this.listner = listner;
        }

        public interface CustomListner{
                 public void highlight();
        }
        LocationListener locationListenerNetwork = new LocationListener() {
            public void onLocationChanged(Location location) {

                locationResult.gotLocation(location);

                listner.highlight();

                lm.removeUpdates(this);
                lm.removeUpdates(locationListenerGps);
            }

            public void onProviderDisabled(String provider) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        };
}



回答3:


Try this way,hope this will help you to solve your problem.

public class MainActivity extends ActionBarActivity implements LocationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyLocation myLocation = new MyLocation(this);
        myLocation.getLocation(this, locationResult);

    }
}

public class MyLocation {

    LocationManager lm;
    LocationResult locationResult;
    private Context context;
    TextView tvnetwork, tvgps;
    private int defaultTextColor;

    public void MyLocation(Context context) {
        this.context = context;
    }

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {

            locationResult.gotLocation(location);

            View v = LayoutInflater.from(context).inflate(R.layout.main, null);

            tvnetwork = (TextView) v.findViewById(R.id.tvnetwork);
            tvgps = (TextView) v.findViewById(R.id.tvgps);
            defaultTextColor = tvgps.getTextColors().getDefaultColor();

            tvnetwork.setTextColor(context.getResources().getColor(
                    R.color.green));
            tvgps.setTextColor(defaultTextColor);

            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
}


来源:https://stackoverflow.com/questions/25807205/findviewbyid-in-non-activity-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!