Java语言空指针异常
我下面的简单代码没有显示错误, 然而,它并没有执行。 致命异常:主 java.lang.RunTimeException:无法启动活动。。。。 java.lang.NullPointerExceptionJava语言空指针异常,java,android,eclipse,Java,Android,Eclipse,我下面的简单代码没有显示错误, 然而,它并没有执行。 致命异常:主 java.lang.RunTimeException:无法启动活动。。。。 java.lang.NullPointerException package com.example.mapstestmednex; import android.app.Activity; import android.location.Location; import android.location.LocationListener; import
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>
有什么问题吗?
谢谢。您从未初始化
公共文本视图;
公共文本视图
你应该这样做
tvLatitude=新文本视图();
首先,您可以稍后在其上设置文本
长时间也一样。
希望这对你有所帮助
tvLatitude = (TextView) findViewById(R.id.tvLatitude);
在访问
tvLatitude
之前,例如设置其文本。同样地,tv经度
检查您的活动\u主布局
。您可能有文本视图来显示纬度和经度。
因此,在setContentView(R.layout.activity_main)之后,添加这两个
tvLatitude = (TextView) findViewById(R.id.lblLatitude);
tvLongitude = (TextView) findViewById(R.id.lblLongitude);
请确保在布局中使用正确的id替换
R.id.lblLatitude
和R.id.lblLongitude
。从LogCat发布堆栈跟踪。如果有效,请接受答案以帮助社区。正确地编写答案,如R.id.lblLatitude那样,而不是“Latitude id here”@Nana@user2026760看起来这里引用了错误的文本视图;非“标签”似乎更受欢迎。e、 g.R.id.tvLatitude
@user2026760不,这行不通。它不引用布局资源中的任何一个文本视图,即使它阻止了NPE。