仍然是相对较新的我在我的活动类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);
}
Run Code Online (Sandbox Code Playgroud)
在这里我在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)); …Run Code Online (Sandbox Code Playgroud) 我想在自定义首选项屏幕中显示首选项的值.为此,我需要在首选项片段中获取值,在本例中为字符串.在非片段(活动)中,我使用eg获取值final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);并获取字符串,String phonenumber = prefs.getString("preference_name", null);但在首选项片段getDefaultSharedPreferences中不适用于首选项片段.
不知道怎么解决这个问题?
这是我的代码片段:
public class PreferencesFragment extends PreferenceFragment implements
OnSharedPreferenceChangeListener {
TextView tvusername, tvphonenumber;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
// entering preference phonenumber in text view
String phonenumber = prefs.getString("phonenumber", null);
;
tvphonenumber.setText(phonenumber);
// entering preference username in text view
String username = prefs.getString("username", null);
;
tvusername.setText(username);
}
Run Code Online (Sandbox Code Playgroud)