findViewById在非Activity类中

时间:2014-09-12 11:25:37

标签: android layout-inflater

对于我来说还是比较新的,我在我的活动类MainActivity中使用的非活动类MyLocation中查找视图时遇到了问题。我使用MyLocation来获取经度和纬度。 我想在使用GPS或网络时突出显示文本视图。为此,我需要在非活动类MyLocation中找到textviews。

以下是我在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);

}

这是我在MyLocation中尝试查找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) {
    }
};

但是找不到意见。我已经获得了NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);。我做错了什么?

3 个答案:

答案 0 :(得分:12)

  

获取NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);.什么   我做错了吗?

因为context类中的nullMyLocation。使用MyLocation类构造函数将MainActivity中的MyLocation上下文传递给访问系统服务:

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

并在MainActivity创建MyLocation类对象,方法是将MainActivity上下文传递为:

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

现在使用context访问MyLocation

中的系统服务

编辑:而不是在onLocationChanged中再次膨胀主布局,而是使用Activity上下文从Activity Layout访问视图:

 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);

       ....
    }

答案 1 :(得分:0)

为什么要在单独的类中执行此操作,因为您在Activity中实现了“LocationListener”。如果你想在单独的课堂上做。您可以通过设置此类

等自定义列表来通知活动
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) {
            }
        };
}

答案 2 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

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) {
        }
    };
}