【问题标题】:Android- Unable to show received location values due to java.lang.NullPointerException [duplicate]Android-由于java.lang.NullPointerException而无法显示接收到的位置值[重复]
【发布时间】:2018-05-16 02:25:15
【问题描述】:

我正在尝试创建一个布局,将在文本视图中显示接收到的位置更新,但我不断收到此错误,我做错了什么?

我们将不胜感激,在此先感谢!

这是错误:

E/AndroidRuntime: 致命异常: main 进程:com.example.defro.app,PID:7731 java.lang.NullPointerException:尝试在空>对象引用上调用虚拟>方法'void android.widget.TextView.setText(java.lang.CharSequence)' 在 com.example.defro.app.MainActivity$1.onLocationChanged(MainActivity.java:29)`

第 29 行是:

txtLoc.setText(String.valueOf(iaLocation.getLongitude() + ", " + iaLocation.getLatitude()));

Main_Activity:

public class MainActivity extends AppCompatActivity {

IALocationManager mLocationManager;
IALocationListener mLocationListener = new IALocationListener() {
    @Override
    public void onLocationChanged(IALocation iaLocation) {
        TextView txtLoc = (TextView) findViewById(R.id.textView);
        txtLoc.setText(String.valueOf(iaLocation.getLongitude() + ", " + iaLocation.getLatitude()));

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

};
private final int CODE_PERMISSIONS = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mLocationManager = IALocationManager.create(this);


    String[] neededPermissions = {
            Manifest.permission.CHANGE_WIFI_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN



    };
    ActivityCompat.requestPermissions(this, neededPermissions, CODE_PERMISSIONS);


}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    //Handle if any of the permissions are denied, in grantResults
}


protected void onResume() {
    super.onResume();
    mLocationManager.requestLocationUpdates(IALocationRequest.create(), mLocationListener);

}

protected void onPause() {
    mLocationManager.removeLocationUpdates(mLocationListener);
    super.onPause();
}

protected void onDestroy() {

   mLocationManager.destroy();
   super.onDestroy();
}

}

content_main.xml:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="23dp"
    android:text="Lat, Long"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.051"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.032" />

【问题讨论】:

  • 如果已经有",",则不需要代码审查String.valueOf()...

标签: java android


【解决方案1】:

在 onCreate 方法中写入这一行

TextView txtLoc = (TextView) findViewById(R.id.textView);

给TextView添加id

android:id="@+id/textView"

【讨论】:

  • 你的答案错了!
  • 你的答案错了..
  • 没有看到 id 丢失 xD EDITED!
  • 很好,然后 +1 进行编辑..
  • 谢谢,这解决了错误,但仍然没有显示接收到的值,屏幕上只显示“lat,long”
【解决方案2】:

您正在将您的 TextView 链接为TextView txtLoc = (TextView) findViewById(R.id.textView);

但是,您的 XML 布局文件中没有定义 ID。 在content_main.xml 中定义您的Textview id,例如android:id="@+id/textView"

【讨论】:

    【解决方案3】:

    乍一看,textView好像没有

    android:id="@+id/textView"
    

    这可能是因为它没有找到视图。此外,您可能希望在 onCreate() 中获取对 textView 的引用,并将其与空检查一起使用。

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 2019-03-08
      • 2017-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2017-09-09
      相关资源
      最近更新 更多