- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 android 中做应用程序以获取我附近的地方,当我尝试运行时我做了一切但得到了这个异常,我不知道是什么原因导致这个异常
主要 Activity
package com.gas;
public class MainActivity extends Activity implements LocationListener{
GoogleMap mGoogleMap;
GoogleMap map;
Spinner mSprPlaceType;
String[] mPlaceType=null;
String[] mPlaceTypeName=null;
double mLatitude=0;
double mLongitude=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlaceType = getResources().getStringArray(R.array.place_type);
mPlaceTypeName = getResources().getStringArray(R.array.place_type_name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, mPlaceTypeName);
mSprPlaceType = (Spinner) findViewById(R.id.spr_place_type);
mSprPlaceType.setAdapter(adapter);
Button btnFind;
btnFind = ( Button ) findViewById(R.id.btn_find);
MapFragment fm=(MapFragment) getFragmentManager().findFragmentById(R.id.map);
map=fm.getMap();
map.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null)
onLocationChanged(location);
locationManager.requestLocationUpdates(provider, 20000, 0, this);
btnFind.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int selectedPosition = mSprPlaceType.getSelectedItemPosition();
String type = mPlaceType[selectedPosition];
StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
sb.append("location="+mLatitude+","+mLongitude);
sb.append("&radius=5000");
sb.append("&types="+type);
sb.append("&sensor=true");
sb.append("&key=YOUR_API_KEY");
PlacesTask placesTask = new PlacesTask();
placesTask.execute(sb.toString());
}
});
}
/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class PlacesTask extends AsyncTask<String, Integer, String>{
String data = null;
@Override
protected String doInBackground(String... url) {
try{
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result){
ParserTask parserTask = new ParserTask();
parserTask.execute(result);
}
}
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
JSONObject jObject;
@Override
protected List<HashMap<String,String>> doInBackground(String... jsonData) {
List<HashMap<String, String>> places = null;
PlaceJSONParser placeJsonParser = new PlaceJSONParser();
try{
jObject = new JSONObject(jsonData[0]);
places = placeJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}
@Override
protected void onPostExecute(List<HashMap<String,String>> list){
mGoogleMap.clear();
for(int i=0;i<list.size();i++){
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> hmPlace = list.get(i);
double lat = Double.parseDouble(hmPlace.get("lat"));
double lng = Double.parseDouble(hmPlace.get("lng"));
String name = hmPlace.get("place_name");
String vicinity = hmPlace.get("vicinity");
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(name + " : " + vicinity);
mGoogleMap.addMarker(markerOptions);
}
}
}
@Override
public void onLocationChanged(Location location) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
LatLng latLng = new LatLng(mLatitude, mLongitude);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}
}
list
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gas"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<permission
android:name="com.gas.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.gas.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:name="android.hardware.location"
android:required="false" />
<uses-feature
android:name="android.hardware.location.network"
android:required="false" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gas.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAtg_enu6Ox2l-WMyT-rpk_dUytQvSZaEw" />
</application>
</manifest>
放置JasonParser
package com.gas;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PlaceJSONParser {
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
JSONArray jPlaces = null;
try {
/** Retrieves all the elements in the 'places' array */
jPlaces = jObject.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getPlaces with the array of json object
* where each json object represent a place
*/
return getPlaces(jPlaces);
}
private List<HashMap<String, String>> getPlaces(JSONArray jPlaces){
int placesCount = jPlaces.length();
List<HashMap<String, String>> placesList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> place = null;
/** Taking each place, parses and adds to list object */
for(int i=0; i<placesCount;i++){
try {
/** Call getPlace with place JSON object to parse the place */
place = getPlace((JSONObject)jPlaces.get(i));
placesList.add(place);
} catch (JSONException e) {
e.printStackTrace();
}
}
return placesList;
}
/** Parsing the Place JSON object */
private HashMap<String, String> getPlace(JSONObject jPlace){
HashMap<String, String> place = new HashMap<String, String>();
String placeName = "-NA-";
String vicinity="-NA-";
String latitude="";
String longitude="";
try {
// Extracting Place name, if available
if(!jPlace.isNull("name")){
placeName = jPlace.getString("name");
}
// Extracting Place Vicinity, if available
if(!jPlace.isNull("vicinity")){
vicinity = jPlace.getString("vicinity");
}
latitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lat");
longitude = jPlace.getJSONObject("geometry").getJSONObject("location").getString("lng");
place.put("place_name", placeName);
place.put("vicinity", vicinity);
place.put("lat", latitude);
place.put("lng", longitude);
} catch (JSONException e) {
e.printStackTrace();
}
return place;
}
}
布局
<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="match_parent"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spr_place_type"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/spr_place_type"
android:text="@string/str_btn_find" />
<fragment
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/spr_place_type"
class="com.google.android.gms.maps.MapFragment" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
日志猫
05-23 04:19:27.233: D/AbsListView(4213): Get MotionRecognitionManager
05-23 04:19:27.573: D/AndroidRuntime(4213): Shutting down VM
05-23 04:19:27.573: W/dalvikvm(4213): threadid=1: thread exiting with uncaught exception (group=0x40c18a68)
05-23 04:19:27.573: E/AndroidRuntime(4213): FATAL EXCEPTION: main
05-23 04:19:27.573: E/AndroidRuntime(4213): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gas/com.gas.MainActivity}: java.lang.NullPointerException
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.os.Looper.loop(Looper.java:137)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-23 04:19:27.573: E/AndroidRuntime(4213): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 04:19:27.573: E/AndroidRuntime(4213): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 04:19:27.573: E/AndroidRuntime(4213): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-23 04:19:27.573: E/AndroidRuntime(4213): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-23 04:19:27.573: E/AndroidRuntime(4213): at dalvik.system.NativeStart.main(Native Method)
05-23 04:19:27.573: E/AndroidRuntime(4213): Caused by: java.lang.NullPointerException
05-23 04:19:27.573: E/AndroidRuntime(4213): at com.gas.MainActivity.onLocationChanged(MainActivity.java:375)
05-23 04:19:27.573: E/AndroidRuntime(4213): at com.gas.MainActivity.onCreate(MainActivity.java:194)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.Activity.performCreate(Activity.java:4470)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-23 04:19:27.573: E/AndroidRuntime(4213): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-23 04:19:27.573: E/AndroidRuntime(4213): ... 11 more
05-23 04:21:46.973: D/AbsListView(4760): Get MotionRecognitionManager
05-23 04:21:47.253: D/AndroidRuntime(4760): Shutting down VM
05-23 04:21:47.253: W/dalvikvm(4760): threadid=1: thread exiting with uncaught exception (group=0x40c18a68)
05-23 04:21:47.253: E/AndroidRuntime(4760): FATAL EXCEPTION: main
05-23 04:21:47.253: E/AndroidRuntime(4760): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gas/com.gas.MainActivity}: java.lang.NullPointerException
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.os.Looper.loop(Looper.java:137)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-23 04:21:47.253: E/AndroidRuntime(4760): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 04:21:47.253: E/AndroidRuntime(4760): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 04:21:47.253: E/AndroidRuntime(4760): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-23 04:21:47.253: E/AndroidRuntime(4760): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-23 04:21:47.253: E/AndroidRuntime(4760): at dalvik.system.NativeStart.main(Native Method)
05-23 04:21:47.253: E/AndroidRuntime(4760): Caused by: java.lang.NullPointerException
05-23 04:21:47.253: E/AndroidRuntime(4760): at com.gas.MainActivity.onLocationChanged(MainActivity.java:375)
05-23 04:21:47.253: E/AndroidRuntime(4760): at com.gas.MainActivity.onCreate(MainActivity.java:194)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.Activity.performCreate(Activity.java:4470)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-23 04:21:47.253: E/AndroidRuntime(4760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-23 04:21:47.253: E/AndroidRuntime(4760): ... 11 more
最佳答案
你有评论
mGoogleMap = fragment.getMap();
当您尝试移动相机时,您的 mGoogleMap
在方法 onLocationChanged
中为 null -> MainActivity 中的第 375 行:
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
关于Android 谷歌地图 : Unable to start activity ComponentInfo MainActivity}: java. lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16704677/
这个问题在这里已经有了答案: android studio adb Syntax error: ")" unexpected (4 个答案) 关闭 5 年前。 我确实将我的工作室更新到 Linux
当我打开 Android Studio 时,它会显示下面的对话框,我的应用程序无法以 Debug模式运行。 最佳答案 找到sdk->platform-tool文件夹,然后运行命令adb tcpip 5
我在 Fedora 33 机器上使用了 VSCode (1.52.1) 一段时间。 我正在使用 Docker 扩展 (v1.9.0) 但由于某种原因,在过去几天中,当我尝试将 VSCode 附加到 D
我正在尝试为我的任务加载一个名为“tr_model.h5”的预训练模型,但出现以下错误: Traceback (most recent call last): File "Trigger_Proje
我最近在 Google Play 中发布了一个 Android 应用程序,到目前为止一切看起来都很棒,但有一个异常(exception)(见下文),我有时会收到控制台崩溃的消息,它只发生在某些用户身上
单击警告时,我收到此消息。这是案件的截图。 错误写道, Unable to open 'warning.cpp': Unable to read file '/Users/dimen/code/C++
重新编译 php 后,当我使用 php cli 时出现以下错误: PHP Warning: PHP Startup: imap: Unable to initialize module Module
使用 git 1.6.4.2,当我尝试 git pull 时出现此错误: error: unable to resolve reference refs/remotes/origin/LT558-op
我是 Kotlin 的新手,我正在学习教程。运行我的应用程序会导致它在运行时崩溃。我在底部导航栏应用程序中有三个 fragment 和一个主要 Activity 。我的目标实际上只是成功运行该应用程序
我在 Windows 10 64 位上运行 Android Studio。我在 5 月 20 日早上升级到了最新的版本和 SDK,从那时起,我在打开 Android Studio 时收到上述错误。我还
因此,我创建了一个 GitHub 操作,该操作应该在发生推送时构建 docker 镜像并将其推送到 docker hub。所以这是我的 GitHub 操作:(第一次创建 GitHub 操作) name
当这些问题中的任何一个都没有帮助我时,这意味着我需要删除并重新安装 Android Studio。 这是错误: Error initializing ADB: Unable to create Deb
Iam running the latest OSX/Flutter/XCode Versions using flutter, android studio and firebase and
我最近重新安装了 ubuntu 20.04.3,在做了一些设置后,我现在在运行 apt update 时总是收到以下错误.我一定是不小心删除了某种缓存文件或目录,但我不知道如何诊断或解决这个问题。 任
今天,当我将更改推送到 Gitlab 中的代码存储库时,我才开始在 Gitlab 中收到此错误消息。 Gitlab fatal: unable to access '.......' SSL Cert
我正在使用 Protractor 和 Jasmine 。 我已经确定了 chrome 驱动程序版本:2.32.498550 (latest)与 Chrome 测试版不兼容 (Version 62.0.
我正在尝试使用 terraform (0.12.24) 和多个 Docker 提供程序(插件版本 2.7.0)执行简单部署。我使用下面的 Terraform 模板的目的是将两个不同的容器部署到两个不同
这个问题我看了很久,最后决定在这里提出来。我需要维护一些用 Delphi 2007 for .NET (ASP.NET 2.0) 编写的应用程序。通常,第一次运行应用程序(使用 IIS)时,我会收到经
如何修复 - 无法创建调试桥:无法启动 adb 服务器:无法检测 adb 版本,adb 输出:/home/dilip/Downloads/sdk/platform-tools/adb: 1:/home
通过命令提示符连接到 android 中的 Sqlite DB 时出现错误。 以下是我遵循的步骤: 我已经在 android 中通过 java 程序创建了 Sqlite 数据库。创建表并向其中插入数据
我是一名优秀的程序员,十分优秀!