[英]Java lang null pointer exception
我下面的简单代码没有显示错误,但没有执行。 致命异常:主要 java.lang.RunTimeException:无法启动活动....java.lang.NullPointerException。
package com.example.mapstestmednex;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private LocationManager lm;
private LocationListener localListenD;
public TextView tvLatitude;
public TextView tvLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Location loc = lm.getLastKnownLocation("gps");
tvLatitude.setText(Double.toString(loc.getLatitude()));
tvLongitude.setText(Double.toString(loc.getLatitude()));
localListenD = new DipsListLocListener();
lm.requestLocationUpdates("gps", 30000L, 10.0f, localListenD);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class DipsListLocListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
tvLatitude.setText(Double.toString(location.getLatitude()));
tvLongitude.setText(Double.toString(location.getLatitude()));
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
}
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/lblLatitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:" />
<TextView
android:id="@+id/tvLatitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/lblLongitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:" />
<TextView
android:id="@+id/tvLongitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
有什么问题? 谢谢。
您永远不会初始化公共 TextView tvLatitude; 公共 TextView tvLongitude;
你应该做 tvLatitude = new TextView(); 首先,您可以稍后在其上设置文本。
tvLongtitude 也是如此。 希望有帮助
你需要
tvLatitude = (TextView) findViewById(R.id.tvLatitude);
在您访问tvLatitude
之前,例如设置其文本。 同样tvLongitude
。
检查您的activity_main layout
。 您可能有文本视图来显示纬度和经度。 所以在setContentView(R.layout.activity_main)之后,添加这两个..
tvLatitude = (TextView) findViewById(R.id.lblLatitude);
tvLongitude = (TextView) findViewById(R.id.lblLongitude);
确保在布局中用正确的 id 替换R.id.lblLatitude
和R.id.lblLongitude
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.