非Activity类中的findViewById

10

作为一个相对新手,我在非活动类MyLocation中找不到视图,在我的活动类MainActivity中使用MyLocation来获取经纬度。我想在GPS或网络被使用时突出显示一个TextView。为此,我需要在非活动类MyLocation中找到这些TextView。

以下是我在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尝试查找文本视图的内容:

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

但是视图没有被找到。我在使用.getSystemService(Context.LAYOUT_INFLATER_SERVICE);时已经收到了NPE错误提示。我做错了什么?


你在哪里定义了 getLocation 方法? - Spring Breaker
非常简单……当您调用MyLocation类时,只需将上下文作为参数添加到其中,然后您就可以从那里获取所有东西。 - GvSharma
3个回答

14

收到一个NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);,我做错了什么?

MyLocation类中context为空。使用MyLocation类的构造函数将MainActivity的上下文传递给MyLocation,以便在MyLocation中访问系统服务,示例:

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

MainActivity中,通过将MainActivity上下文作为参数传递来创建MyLocation类对象:

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

现在使用context来访问MyLocation类中的系统服务

EDIT: 代替在onLocationChanged中再次膨胀主布局,请使用Activity上下文来访问Activity布局中的视图,如下所示:

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

       ....
    }

太好了,非常感谢,不再有NPE错误了。但是我的TextView颜色没有改变。有什么提示吗? - sascha

1

尝试这种方式,希望能帮助您解决问题。

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

0
为什么要在单独的类中实现“LocationListener”,而不是直接在Activity中实现呢?如果您确实想要使用单独的类,可以通过设置自定义监听器来通知Activity,例如:
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) {
            }
        };
}

非常感谢您的解决方案,但我更喜欢将类分开,因为这可以使我的活动更清晰,并且我可以更轻松地在另一个项目中重用它。再次感谢! - sascha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接