Android:尝试从其他 class 访问 TextView 时出现 NullPointerException
Android: NullPointerException when trying to access a TextView from other class
我目前正在尝试学习 android 开发。现在我有问题我找不到解决方案。
应用程序应获取 gps 位置并将坐标写入文本视图。
但是当 gps 收到信号时,我收到 NullPointerException。
以下代码来自我的 MainActitvity。 GetLocation 是我的 class 获取 gps 坐标。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
GetLocation getMyLocation = new GetLocation();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, getMyLocation);
}
这是我的 GetLocation class 中发生错误的部分。错误发生在 findViewById
行
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String myLocation = location.getLatitude() + " " + location.getLongitude();
viewToChange = (TextView) this.activity.findViewById(R.id.my_position);
viewToChange.setText("Position " + myLocation);
}
我想我无法从 GetLocation 中的主活动访问布局 class...:/
更新:
public class GetLocation implements LocationListener {
TextView viewToChange;
private Activity activity;
private void onCreate(){
}
public void GetLocation(Activity _activity){
this.activity = _activity;
}
@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String myLocation = location.getLatitude() + " " + location.getLongitude();
viewToChange = (TextView) this.activity.findViewById(R.id.my_position);
viewToChange.setText("Position " + myLocation);
}
由于 NullPointerException
发生在这里:
viewToChange = (TextView) this.activity.findViewById(R.id.my_position);
是activity
即null
,不是viewToChange
。确保您已正确传递 activity
对象。
据我所知,没有标准的方法可以从另一个 class 中检索您的 Activity。你可以(选择一个):
- 将 TextView 传递给您的方法。
- 将您的方法 return 设为 String 并在您的主 activity 上设置您的 TextView。
- 将上下文传递给您的 class 构造函数并正确检索您的 activity
- 传递你的 activity 意图。
如果您不提供 classes 的代码和堆栈跟踪,则很难查明问题所在。我猜因为 this.activity
在您的 GetLocation
class 中在句法上是正确的,它具有 activity 作为属性。确保此 activity 变量不为空并且 activity 已设置其内容。
我想删除 "this.activity.findViewById" 上的 "this" 将解决您的问题。创建一个全局 activity 并在您的构造函数上初始化它并在没有 "this".
的情况下使用它
我还可以向您推荐更有效的方法来自定义来自其他 classes 的视图:
使视图 public 静态是一个选项:
public class MainActivity extends Activity{
public static TextView textViewToChange;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewToChange = (TextView) findViewById(R.id.textViewToChange);
}
}
在您的 GetLocation class 中,您可以通过以下方式到达它:
MainActivity.textViewToChange.setText("someText");
您可以通过构造函数将视图传递给 GetLocation:
public GetLocation(TextView view){
//create a global variable and store
this.view = view
}
并在 onLocationChanged 方法中更改其文本。
您还可以创建一个接口并让您的 MainActivity 实现它,并在您进行更改时从 GetLocation 触发它:
接口:
public interface FooChangeListener {
void onFooChange(String text);
}
您的主要活动:
public class MainActivity extends Activity implements FooChangeListener {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
}
@Override
public void onFooChange(String text) {
tv.setText(text);
}
}
在您的 GetLocation class 和您的 GetLocation 构造函数中创建一个全局 FooChangeListener:
public GetLocation(FooChangeListener listener){
this.listener = listener;
}
并且在您的 onLocationChanged 中:
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String myLocation = location.getLatitude() + " " + location.getLongitude();
listener.onFooChange(myLocation);
}
以上就是我的建议,希望对您有所帮助!
我目前正在尝试学习 android 开发。现在我有问题我找不到解决方案。
应用程序应获取 gps 位置并将坐标写入文本视图。
但是当 gps 收到信号时,我收到 NullPointerException。
以下代码来自我的 MainActitvity。 GetLocation 是我的 class 获取 gps 坐标。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
GetLocation getMyLocation = new GetLocation();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, getMyLocation);
}
这是我的 GetLocation class 中发生错误的部分。错误发生在 findViewById
行 public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String myLocation = location.getLatitude() + " " + location.getLongitude();
viewToChange = (TextView) this.activity.findViewById(R.id.my_position);
viewToChange.setText("Position " + myLocation);
}
我想我无法从 GetLocation 中的主活动访问布局 class...:/
更新:
public class GetLocation implements LocationListener {
TextView viewToChange;
private Activity activity;
private void onCreate(){
}
public void GetLocation(Activity _activity){
this.activity = _activity;
}
@Override
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String myLocation = location.getLatitude() + " " + location.getLongitude();
viewToChange = (TextView) this.activity.findViewById(R.id.my_position);
viewToChange.setText("Position " + myLocation);
}
由于 NullPointerException
发生在这里:
viewToChange = (TextView) this.activity.findViewById(R.id.my_position);
是activity
即null
,不是viewToChange
。确保您已正确传递 activity
对象。
据我所知,没有标准的方法可以从另一个 class 中检索您的 Activity。你可以(选择一个):
- 将 TextView 传递给您的方法。
- 将您的方法 return 设为 String 并在您的主 activity 上设置您的 TextView。
- 将上下文传递给您的 class 构造函数并正确检索您的 activity
- 传递你的 activity 意图。
如果您不提供 classes 的代码和堆栈跟踪,则很难查明问题所在。我猜因为 this.activity
在您的 GetLocation
class 中在句法上是正确的,它具有 activity 作为属性。确保此 activity 变量不为空并且 activity 已设置其内容。
我想删除 "this.activity.findViewById" 上的 "this" 将解决您的问题。创建一个全局 activity 并在您的构造函数上初始化它并在没有 "this".
的情况下使用它我还可以向您推荐更有效的方法来自定义来自其他 classes 的视图:
使视图 public 静态是一个选项:
public class MainActivity extends Activity{
public static TextView textViewToChange;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewToChange = (TextView) findViewById(R.id.textViewToChange);
}
}
在您的 GetLocation class 中,您可以通过以下方式到达它:
MainActivity.textViewToChange.setText("someText");
您可以通过构造函数将视图传递给 GetLocation:
public GetLocation(TextView view){
//create a global variable and store
this.view = view
}
并在 onLocationChanged 方法中更改其文本。
您还可以创建一个接口并让您的 MainActivity 实现它,并在您进行更改时从 GetLocation 触发它:
接口:
public interface FooChangeListener {
void onFooChange(String text);
}
您的主要活动:
public class MainActivity extends Activity implements FooChangeListener {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
}
@Override
public void onFooChange(String text) {
tv.setText(text);
}
}
在您的 GetLocation class 和您的 GetLocation 构造函数中创建一个全局 FooChangeListener:
public GetLocation(FooChangeListener listener){
this.listener = listener;
}
并且在您的 onLocationChanged 中:
public void onLocationChanged(Location location) {
location.getLatitude();
location.getLongitude();
String myLocation = location.getLatitude() + " " + location.getLongitude();
listener.onFooChange(myLocation);
}
以上就是我的建议,希望对您有所帮助!