【问题标题】:findViewById in non-Activity class非 Activity 类中的 findViewById
【发布时间】:2014-11-06 13:39:13
【问题描述】:

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

这是我在 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) {
    }
};

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

【问题讨论】:

  • 你在哪里定义了getLocation 方法?
  • 非常简单...当您调用 MyLocation 类时...只需将上下文作为参数添加到它,这样您就可以从那里获取所有内容。

标签: android layout-inflater


【解决方案1】:

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

因为contextMyLocation 类中是null。使用MyLocation 类构造函数在MyLocation 中传递MainActivity 上下文以访问系统服务:

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 类中的系统服务

编辑:不要在 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 不会改变颜色。有什么提示吗?
【解决方案2】:

试试这个方法,希望能帮助你解决问题。

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

【讨论】:

    【解决方案3】:

    为什么要在单独的类中执行此操作,因为您正在 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) {
                }
            };
    }
    

    【讨论】:

    • 非常感谢您的解决方案,但我更喜欢将班级分开,因为这样可以让我的活动更干净,我可以更轻松地将其重用于另一个项目;-) 再次感谢!
    猜你喜欢
    • 2012-01-09
    • 2013-04-15
    • 2021-06-27
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    相关资源
    最近更新 更多