问题描述
我想测试一个新的手机,我刚刚与Android 4.2购买我的Android应用程序。它运行所有其他应用程序,我的测试,而这个特定的应用程序也适用于其他设备。当我尝试运行code它给了我以下错误:
I'm trying to test my Android app on a new phone I've just purchased with Android 4.2. It runs every other app I test, and this particular app also works on other devices. When I try to run the code it's giving me the following errors:
> 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这个错误。
Now I've checked line 127 and even removing that and leaving an empty line will still cause this error relating to line 127.
我MainActivity.class code:
My MainActivity.class code:
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;
}
}
}
我觉得很奇怪它是如何工作的一台设备上,然后崩溃在另一个上。
I find it strange how it works on one device and then crashes on another.
任何帮助的感谢! :)
Thanks for any help! :)
编辑:
activity_layout.xml文件:
activity_layout.xml file:
<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文件:
AndroidManifest.xml file:
不知道为什么,但它不喜欢的缩进。
Not sure why, but it isn't liking the indents.
所以对于链接抱歉,但是:
So sorry for the link but:
推荐答案
我觉得你的
Location myLocation = locationManager.getLastKnownLocation(provider);
行返回null。
line returning null.
请检查答案。它可能会有所帮助。
Please check this answer. It might be helpful
这篇关于运行时异常无法启动MainActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!