我正在尝试使用Android 4.2购买的新手机上测试我的Android应用。它运行我测试的每个其他应用程序,这个特定的应用程序也适用于其他设备。当我尝试运行代码时,它会给我以下错误:
> 02-23 12:15:08.532: E/AndroidRuntime(5431): FATAL EXCEPTION: main
02-23 12:15:08.532: E/AndroidRuntime(5431): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.antiquity/com.example.antiquity.MainActivity}: java.lang.NullPointerException
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.os.Handler.dispatchMessage(Handler.java:107)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.os.Looper.loop(Looper.java:194)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.ActivityThread.main(ActivityThread.java:5371)
02-23 12:15:08.532: E/AndroidRuntime(5431): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:15:08.532: E/AndroidRuntime(5431): at java.lang.reflect.Method.invoke(Method.java:525)
02-23 12:15:08.532: E/AndroidRuntime(5431): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-23 12:15:08.532: E/AndroidRuntime(5431): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-23 12:15:08.532: E/AndroidRuntime(5431): at dalvik.system.NativeStart.main(Native Method)
02-23 12:15:08.532: E/AndroidRuntime(5431): Caused by: java.lang.NullPointerException
02-23 12:15:08.532: E/AndroidRuntime(5431): at com.example.antiquity.MainActivity.setUpLocation(MainActivity.java:127)
02-23 12:15:08.532: E/AndroidRuntime(5431): at com.example.antiquity.MainActivity.onCreate(MainActivity.java:96)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.Activity.performCreate(Activity.java:5139)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
02-23 12:15:08.532: E/AndroidRuntime(5431): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
02-23 12:15:08.532: E/AndroidRuntime(5431): ... 11 more
现在我已经检查了第127行,甚至删除它并留下一个空行仍然会导致与第127行有关的错误。
我的MainActivity.class代码:
public class MainActivity extends Activity {
private GoogleMap map;
Intent data;
MonumentsDatabase monDatabase;
int _id;
int _lat;
int _lng;
String _title;
LatLng MARKERS;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent data = new Intent(MainActivity.this, DisplayData.class);
startActivity(data);
}
});
setUpLocation();
locationManager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
disabledGPS();
}
map.setMyLocationEnabled(true);
}
public void setUpLocation() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location myLocation = locationManager.getLastKnownLocation(provider);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
map.animateCamera(CameraUpdateFactory.zoomTo(40));
}
private void disabledGPS() {
final AlertDialog.Builder gpsDisabled = new AlertDialog.Builder(this);
gpsDisabled.setMessage("This app requires GPS to be enabled. Do you wish to continue?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog mainAlert = gpsDisabled.create();
mainAlert.show();
}
/**
public boolean onMarkerClick(Marker marker) {
//Intent data = new Intent(this, DisplayData.class);
// TODO Auto-generated method stub
startActivity(data);
return true;
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void createDatabase() {
monDatabase = new MonumentsDatabase(this);
try {
monDatabase.createDatabase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
monDatabase.openDatabase();
}catch(SQLException sqle){
throw sqle;
}
}
}
我觉得奇怪的是它如何在一台设备上运行然后在另一台设备上崩溃。
感谢您的帮助! :)
编辑:
activity_layout.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
AndroidManifest.xml文件:
不确定原因,但不喜欢缩进。
很抱歉链接但是:
答案 0 :(得分:3)
我认为你的
Location myLocation = locationManager.getLastKnownLocation(provider);
返回null。
请检查this回答。这可能会有所帮助
答案 1 :(得分:2)
此处,如果您要定位最低SDK版本&lt;在您的清单文件中,然后您无法使用 MapFragment 。因此,必须确保您的最低SDK版本高于或等于12.如果不是,则从您的xml文件中更改
android:name="com.google.android.gms.maps.MapFragment"
到
android:name="com.google.android.gms.maps.SupportMapFragment"
并且您的活动也会从
更改 map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
到
map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
此外,如果你在旧设备上使用片段(蜂窝前),你应该始终从Activity
扩展FragmentActivity
。
另外import android.support.v4.app.FragmentActivity
有关此问题的更多信息,请转到我的answer
答案 2 :(得分:1)
如果我没有弄错,应在活动中使用片段,该片段可以扩展Fragment
或FragmentActivity
。 map