非活动类中的findViewById [英] findViewById in non-Activity class

查看:91
本文介绍了非活动类中的findViewById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然相对较新,在我在活动类MainActivity中使用的非活动类MyLocation中查找视图时遇到了问题.我正在使用MyLocation获取经度和纬度. 我想在使用GPS或网络时突出显示文本视图.为此,我需要在非活动类MyLocation中找到文本视图.

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.

这是我在MainActivity中称呼它的方式:

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

}

以下是我在MyLocation中尝试找到的文本视图:

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

但是找不到视图.我已经得到了NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);.我在做什么错了?

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

推荐答案

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

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

因为MyLocation类中的contextnull.使用MyLocation类构造函数在MyLocation中传递MainActivity上下文,以如下方式访问系统服务:

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

并在MainActivity中通过将MainActivity上下文传递为来创建MyLocation类对象:

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

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

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

Now use context to access system services in MyLocation class

编辑:与其在onLocationChanged中再次膨胀主布局,还不如使用Activity上下文以以下方式从Activity Layout访问视图:

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

       ....
    }

这篇关于非活动类中的findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆