应用程序可以在我的真实设备上正常运行,它可以启动并在模拟器上运行,但是,当我登录并到达onCreate方法时,它会抛出错误,如下所示。仿真器已经设置了所有必需的规范,例如正确的API级别和允许使用SD卡作为内存(200 mib)。是否有任何关于如何缓解此错误的见解?
06-24 10:19:53.060: E/AndroidRuntime(625): FATAL EXCEPTION: main
06-24 10:19:53.060: E/AndroidRuntime(625): Process: com.example.bestapp, PID: 625
06-24 10:19:53.060: E/AndroidRuntime(625): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bestapp/com.example.best.MainActivity}: java.lang.NullPointerException
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.ActivityThread.access$800(ActivityThread.java:138)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.os.Handler.dispatchMessage(Handler.java:102)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.os.Looper.loop(Looper.java:136)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.ActivityThread.main(ActivityThread.java:5042)
06-24 10:19:53.060: E/AndroidRuntime(625): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:19:53.060: E/AndroidRuntime(625): at java.lang.reflect.Method.invoke(Method.java:515)
06-24 10:19:53.060: E/AndroidRuntime(625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:776)
06-24 10:19:53.060: E/AndroidRuntime(625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-24 10:19:53.060: E/AndroidRuntime(625): at dalvik.system.NativeStart.main(Native Method)
06-24 10:19:53.060: E/AndroidRuntime(625): Caused by: java.lang.NullPointerException
06-24 10:19:53.060: E/AndroidRuntime(625): at com.example.bestapp.MainActivity.onCreate(MainActivity.java:59)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.Activity.performCreate(Activity.java:5231)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-24 10:19:53.060: E/AndroidRuntime(625): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
MainActivity onCreate方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
Context context;
// Just test displaying
lat = (float) (location.getLatitude());
lng = (float) (location.getLongitude());
// Set up the action bar to show a dropdown list.
final android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
// Populating the dropdown menu
ArrayList<String> itemList = new ArrayList<String>();
// Get the present day
Calendar calendar = Calendar.getInstance();
int today = calendar.get(Calendar.DAY_OF_WEEK);
itemList.add("Today");
if (today == 2) {
itemList.add("Tues");
itemList.add("Weds");
itemList.add("Thurs");
itemList.add("Fri");
}
if (today == 3) {
itemList.add("Weds");
itemList.add("Thurs");
itemList.add("Fri");
itemList.add("Sat");
}
if (today == 4) {
itemList.add("Thurs");
itemList.add("Fri");
itemList.add("Sat");
itemList.add("Sun");
}
if (today == 5) {
itemList.add("Fri");
itemList.add("Sat");
itemList.add("Sun");
itemList.add("Mon");
}
if (today == 6) {
itemList.add("Sat");
itemList.add("Sun");
itemList.add("Mon");
itemList.add("Tues");
}
if (today == 7) {
itemList.add("Sun");
itemList.add("Mon");
itemList.add("Tues");
itemList.add("Weds");
}
if (today == 1) {
itemList.add("Mon");
itemList.add("Tues");
itemList.add("Weds");
itemList.add("Thurs");
}
ArrayAdapter<String> aAdpt = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
itemList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view
.findViewById(android.R.id.text1);
text.setTextColor(Color.WHITE);
return view;
}
};
// Assign list to actionbar
actionBar.setListNavigationCallbacks(aAdpt, null);
}
答案 0 :(得分:0)
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// get last location
Location lastLoc = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLoc == null) {
lastLoc = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
double lastlat = lastLoc.getLatitude();
double lastlng = lastLoc.getLongitude();
LatLng lastLatLng = new LatLng(lastlat, lastlng);
//如果您需要Google地图的示例
userMarker = map.addMarker(new MarkerOptions().position(lastLatLng)
.title("You are here")
.icon(BitmapDescriptorFactory.fromResource(userIcon))
.snippet("Your last recorded location"));