获取位置时出现 java.lang.NullPointerException
我遇到了一些问题,无法在模拟器中重新创建,但我不断在 Android 市场中收到错误报告,我不知道如何解决...其他任何可能看到它的人都会非常高兴
有时此reposrts
java.lang.NullPointerException
at android.webkit.WebViewDatabase.getCacheTotalSize(WebViewDatabase.java:734)
at android.webkit.CacheManager.trimCacheIfNeeded(CacheManager.java:548)
at android.webkit.WebViewWorker.handleMessage(WebViewWorker.java:190)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.os.HandlerThread.run(HandlerThread.java:60)
有时此报告
java.lang.RuntimeException: Unable to start activity ComponentInfo{polis.koll/polis.koll.WebPageLoader}: java.lang.IllegalArgumentException: provider==null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$2500(ActivityThread.java:129)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: provider==null
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:625)
at polis.koll.WebPageLoader.getLocation(WebPageLoader.java:54)
at polis.koll.WebPageLoader.onCreate(WebPageLoader.java:70)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
这是我的代码
public class WebPageLoader extends Activity implements LocationListener{
public static String Android_ID = null;
final Activity activity = this;
private Location mostRecentLocation;
private void CheckEnableGPS(){
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
//GPS Enabled
getLocation();
}else{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
private void getLocation() {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestprovider = locationManager.getBestProvider(criteria,true);
locationManager.requestLocationUpdates(bestprovider, 1, 500, this);
mostRecentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}else{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
//AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } );
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
getLocation();
Android_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
/** Allows JavaScript calls to access application resources **/
webView.addJavascriptInterface(new JavaScriptInterface(), "android16");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Letar poliskontroller");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
// Testade att ta bort denna override för att få market länkar att fungera
// @Override
// public boolean shouldOverrideUrlLoading(WebView view, String url)
// {
// view.loadUrl(url);
// return true;
// }
});
if (Locale.getDefault().getLanguage().equals("sv")){
//webView.loadUrl("file:///android_asset/android.html");
webView.loadUrl("file:///android_asset/findgps_sv.html");
}else{
//webView.loadUrl("http://m.bryggplatsen.se/android/polis/index.php");
//webView.loadUrl("file:///android_asset/android_en.html");
webView.loadUrl("file:///android_asset/findgps_en.html");
}
}
/** Sets up the interface for getting access to Latitude and Longitude data from device
**/
private class JavaScriptInterface {
public double getLatitude(){
return mostRecentLocation != null ? mostRecentLocation.getLatitude() : Double.NaN;
}
public double getLongitude(){
return mostRecentLocation != null ? mostRecentLocation.getLongitude() : Double.NaN;
}
public String getAndroid_ID(){
return Android_ID;
}
public void sharethisapp(){
startActivity(Intent.createChooser(sharespotIntent(), "Share this warning"));
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
getLocation();
//CheckEnableGPS();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
getLocation();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
getLocation();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
getLocation();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add("Shut down");
item.setIcon(R.drawable.exit);
item = menu.add("Share");
item.setIcon(R.drawable.m_share);
item = menu.add("Fix GPS");
item.setIcon(R.drawable.globe);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle() == "Shut down") {
System.exit(0);
finish();
}
if (item.getTitle() == "Fix GPS") {
getLocation();
}
if (item.getTitle() == "Share") {
if (Locale.getDefault().getLanguage().equals("sv")){
startActivity(Intent.createChooser(shareIntent(), "Dela denna app"));
}else{
startActivity(Intent.createChooser(shareIntent(), "Share this app"));
}
}
return true;
}
private Intent shareIntent() {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
if (Locale.getDefault().getLanguage().equals("sv")){
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Testa denna android app...");
return shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "index.php");
}else{
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Try this cool android app...");
return shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "index_en.php");
}
}
private Intent sharespotIntent() {
Intent sharespotIntent = new Intent(android.content.Intent.ACTION_SEND);
sharespotIntent.setType("text/plain");
if (Locale.getDefault().getLanguage().equals("sv")){
sharespotIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " la just upp en ny varning i appen Polisradar");
return sharespotIntent.putExtra(android.content.Intent.EXTRA_TEXT, "warn.php?lat=" + mostRecentLocation.getLatitude() + "&lng=" + mostRecentLocation.getLongitude() + "");
}else{
sharespotIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " just added a new warning in the app Policeradar");
return sharespotIntent.putExtra(android.content.Intent.EXTRA_TEXT, "warn_en.php?lat=" + mostRecentLocation.getLatitude() + "&lng=" + mostRecentLocation.getLongitude() + "");
}
}
}
I have some problems im not able to recreate in emulator, but i keep getting error reports in android market that i have no clue how to solve... anyone else that might see it id be wery greatful
Sometimes this reposrts
java.lang.NullPointerException
at android.webkit.WebViewDatabase.getCacheTotalSize(WebViewDatabase.java:734)
at android.webkit.CacheManager.trimCacheIfNeeded(CacheManager.java:548)
at android.webkit.WebViewWorker.handleMessage(WebViewWorker.java:190)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.os.HandlerThread.run(HandlerThread.java:60)
And sometimes this report
java.lang.RuntimeException: Unable to start activity ComponentInfo{polis.koll/polis.koll.WebPageLoader}: java.lang.IllegalArgumentException: provider==null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
at android.app.ActivityThread.access$2500(ActivityThread.java:129)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: provider==null
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:625)
at polis.koll.WebPageLoader.getLocation(WebPageLoader.java:54)
at polis.koll.WebPageLoader.onCreate(WebPageLoader.java:70)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
Here is my code
public class WebPageLoader extends Activity implements LocationListener{
public static String Android_ID = null;
final Activity activity = this;
private Location mostRecentLocation;
private void CheckEnableGPS(){
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
//GPS Enabled
getLocation();
}else{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
private void getLocation() {
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestprovider = locationManager.getBestProvider(criteria,true);
locationManager.requestLocationUpdates(bestprovider, 1, 500, this);
mostRecentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}else{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
//AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } );
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
getLocation();
Android_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
/** Allows JavaScript calls to access application resources **/
webView.addJavascriptInterface(new JavaScriptInterface(), "android16");
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Letar poliskontroller");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
// Handle the error
}
// Testade att ta bort denna override för att få market länkar att fungera
// @Override
// public boolean shouldOverrideUrlLoading(WebView view, String url)
// {
// view.loadUrl(url);
// return true;
// }
});
if (Locale.getDefault().getLanguage().equals("sv")){
//webView.loadUrl("file:///android_asset/android.html");
webView.loadUrl("file:///android_asset/findgps_sv.html");
}else{
//webView.loadUrl("http://m.bryggplatsen.se/android/polis/index.php");
//webView.loadUrl("file:///android_asset/android_en.html");
webView.loadUrl("file:///android_asset/findgps_en.html");
}
}
/** Sets up the interface for getting access to Latitude and Longitude data from device
**/
private class JavaScriptInterface {
public double getLatitude(){
return mostRecentLocation != null ? mostRecentLocation.getLatitude() : Double.NaN;
}
public double getLongitude(){
return mostRecentLocation != null ? mostRecentLocation.getLongitude() : Double.NaN;
}
public String getAndroid_ID(){
return Android_ID;
}
public void sharethisapp(){
startActivity(Intent.createChooser(sharespotIntent(), "Share this warning"));
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
getLocation();
//CheckEnableGPS();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
getLocation();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
getLocation();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
getLocation();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add("Shut down");
item.setIcon(R.drawable.exit);
item = menu.add("Share");
item.setIcon(R.drawable.m_share);
item = menu.add("Fix GPS");
item.setIcon(R.drawable.globe);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle() == "Shut down") {
System.exit(0);
finish();
}
if (item.getTitle() == "Fix GPS") {
getLocation();
}
if (item.getTitle() == "Share") {
if (Locale.getDefault().getLanguage().equals("sv")){
startActivity(Intent.createChooser(shareIntent(), "Dela denna app"));
}else{
startActivity(Intent.createChooser(shareIntent(), "Share this app"));
}
}
return true;
}
private Intent shareIntent() {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
if (Locale.getDefault().getLanguage().equals("sv")){
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Testa denna android app...");
return shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "index.php");
}else{
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Try this cool android app...");
return shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "index_en.php");
}
}
private Intent sharespotIntent() {
Intent sharespotIntent = new Intent(android.content.Intent.ACTION_SEND);
sharespotIntent.setType("text/plain");
if (Locale.getDefault().getLanguage().equals("sv")){
sharespotIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " la just upp en ny varning i appen Polisradar");
return sharespotIntent.putExtra(android.content.Intent.EXTRA_TEXT, "warn.php?lat=" + mostRecentLocation.getLatitude() + "&lng=" + mostRecentLocation.getLongitude() + "");
}else{
sharespotIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, " just added a new warning in the app Policeradar");
return sharespotIntent.putExtra(android.content.Intent.EXTRA_TEXT, "warn_en.php?lat=" + mostRecentLocation.getLatitude() + "&lng=" + mostRecentLocation.getLongitude() + "");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果请求的名称不在数据库中,Settings.Secure.getString() 方法可以返回 null。但我不知道为什么找不到 Settings.Secure.LOCATION_PROVIDERS_ALLOWED 。但是,您的代码在检查提供程序不是空字符串之前不会检查(在 getLocation() 中)提供程序不为空。我认为这就是你的第二个例外的根源。
框架深处看似随机的错误也可能是由于尝试在不支持代码中某些内容的操作系统版本上运行应用程序而引起的。确保您的代码根据您在清单中设置的 minSdkVersion 进行编译,而不仅仅是 targetSdkVersion。
The method Settings.Secure.getString() can return null if the requested name is not in the data base. I don't know why Settings.Secure.LOCATION_PROVIDERS_ALLOWED wouldn't be found, though. However, your code does not check (in getLocation()) that provider isn't null before checking that it isn't the empty string. I think that's the source of your second exception.
Seemingly random errors deep in the framework can also be caused by trying to run your app on an OS release that doesn't support something in your code. Make sure that your code compiles against the minSdkVersion you have set in your manifest, not just the targetSdkVersion.
关于这个问题的一点说明。我不知道这是否是您的问题,但我正在考虑在应用程序之间共享一些文件并尝试修改 android:sharedUserId 属性。一旦我这样做了,我就得到了与您报告的完全相同的错误,并且我的应用程序无法正常启动(似乎是由于 AdRequest 中的失败)。即使从清单中删除sharedUserId属性后,我仍然收到错误,尽管我认为它来自首选项数据库。
最重要的是,一旦部署应用程序,就不要更改应用程序的sharedUserId 属性,否则您将需要卸载/重新安装周期才能使所有内容再次正常工作。
One note on this issue. I don't know if this was your problem or not but I was looking at sharing some files between applications and tried modifying the android:sharedUserId attribute in my AndroidManifest.xml file. Once I did that I got exactly the same error you reported and my app failed to start up properly (due to a failure in AdRequest, it seems). Even after removing the sharedUserId attribute from the manifest I continued to get errors, though I think it was from the preferences database.
The bottom line is don't change the sharedUserId attribute of an application once it has been deployed or you will require an uninstall/reinstall cycle to get everything working again.