2014-09-12 5 views
7

여전히 비교적 새로운데, 내 활동 클래스 MainActivity에서 사용하고있는 비 활동 클래스 MyLocation에서보기를 찾는 데 문제가 있습니다. 나는 경도와 위도를 얻기 위해 MyLocation을 사용하고있다. GPS 또는 네트워크가 사용 된 경우 텍스트보기를 강조 표시하려고합니다. 그렇게하려면 비 활동 클래스 인 MyLocation에서 textviews를 찾아야합니다. 나는 textviews을 찾기 위해 MyLocation에서 재판을 여기에서비 활동 클래스의 findViewById

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

} 

그리고 :

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

그러나보기는 발견되지 여기

내가 MainActivity에 전화 해요 방법이다. 이미 NPE @.getSystemService(Context.LAYOUT_INFLATER_SERVICE);이 있습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

여기서'getLocation' 메소드를 정의 했습니까? –

+0

매우 간단 ... MyLocation 클래스를 호출 할 때 ... 매개 변수로 컨텍스트를 추가하면 거기에서 모든 것을 얻을 수 있습니다. – GvSharma

답변

11

get NAME @ .getSystemService (Context.LAYOUT_INFLATER_SERVICE) ;. 무엇이 내가 잘못 했나요?

context 때문에 MyLocation 클래스 null입니다. 같은 시스템 서비스에 액세스 할 수 MyLocationMainActivity 전달하는 컨텍스트를 MyLocation 클래스 생성자를 사용

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

MainActivity에로 MainActivity 컨텍스트를 전달하여 MyLocation 클래스의 객체를 생성 : 이제 시스템 서비스에 액세스 할 수 context를 사용

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

MyLocation 클래스

EDIT : 팽창 대신

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

     .... 
    } 
+0

대단히 고마워요, 더 이상 NPE가 없습니다. 하지만 내 textview는 색상을 변경하지 않습니다. 이것에 대한 어떤 힌트? – sascha

+0

@sascha : 내 편집 답변보기 –

+0

그 트릭을 했어, 많이 고마워! – sascha

0

당신이 당신의 자체 활동에서 "LocationListener"을 구현하고 별도의 클래스를하고 싶은 이유 : onLocationChanged 사용 활동의 맥락에서 다시 메인 레이아웃으로 활동 레이아웃에서보기에 액세스 할 수 있습니다. 그리고 당신이 별도의 수업에서하고 싶다면. 당신은이 방법을 사용해보십시오이

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

고맙습니다. 솔루션을 많이 가져 주셔서 감사합니다. 클래스를 구분하여 작업을 깨끗하게 유지하고 다른 프로젝트에서 쉽게 다시 사용할 수 있습니다 .-- 다시 한번 감사드립니다! – sascha

0

같은 설정을 사용자 정의 listners에 의해 활동을 알릴 수 있습니다, 이것은 당신이 당신의 문제를 해결하는 데 도움이되기를 바랍니다.

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) { 
     } 
    }; 
} 
관련 문제