- 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 设置 JUnit 测试并且我有 list 等设置但我不断收到此错误: java.lang.RuntimeException: Unable to instantiate
我的问题是当我运行应用程序并单击注册按钮时突然显示弹出窗口:“强制关闭” 这是我的代码: Main.java package com.example.server; import java.util.
当我运行我的 Activity registeration.java 或 login.java 时,它将 Intent 中的用户名传递给名为 profile.java 的新 Activity > 关于
这个问题已经有答案了: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo (47 个回答) 已关闭 8
以下 Android Studio 1.5 MainActivity.java 代码会在标题中生成错误,并带有 You need to use a Theme.AppCompat theme publ
尝试从文本消息 URL 在 Android 上启动 Xamarin 表单应用程序时,我收到此错误。我一直在遵循 THIS 中提到的步骤文章。 这里是我 AppManifest.xml 中的应用程序节点
我在我的应用程序中创建了一个 map View ,但出现了这个错误: 09-07 21:24:08.886: INFO/ActivityManager(243): Starting: Intent {
我的应用程序运行正常,但当应用程序进入后台时,它在应该恢复时崩溃了。正如您在源代码中看到的那样,我记录了 onStart、onStop 等事件。 在我的日志中,当我启动应用程序时,我可以看到 onSt
我正在处理从其他人那里导入的 Android 项目。我已经对所有依赖项进行排序,项目中没有错误,但是当我尝试启动它时,我得到: 04-08 16:49:41.761: E/AndroidRuntime
在我的应用程序中,当我选择按钮时,它会将我指向 mapView 类,它会显示用户的当前位置。 但是,它抛出 java.lang.RuntimeException: Unable to instanti
我在开发 Android 应用程序时遇到问题。我首先对我的错误进行了研究,然后发现还有其他人和我有同样的问题。我阅读了所有评论并尝试了所有方法,但我仍然遇到同样的错误。 这里是我的错误 05-29 1
我有一个 Google 云消息的接收器,它已在 AndroidManifest.xml 中注册:
我正在开发一个应用程序,最低版本为 Froyo,目标版本为 Gingerbread。因此, list 显示: 我有一个模拟器和一个带有 Gingerbread 的 Nexus One,应用程序部署和
我正在尝试将其作为我的主要 Activity 以打开另一个 Activity (ListAtivity)。但是,当我单击按钮时,应用程序崩溃并出现以下异常: 01-26 17:12:58.341: E
我正在制作一个可以搜索表“员工”并返回结果的应用程序。我正在使用 searchdialog 进行搜索。但是我在搜索对话框上按下搜索按钮时得到了 FC。请帮帮我。 代码: public class Si
我是 Android 开发的新手,所以我确信这会变成我错过的愚蠢的事情,但我有两个 Activity :StartScreen 和 Map。 StartScreen 只是简单的文本和一个按钮,然后应该
我修改了来自 http://www.elektor.com/magazines/2012/march/android-switch-interface.2084156.lynkx 的示例蓝牙通信应用程
我创建了一个应用程序,该应用程序应该从我的Thingspeak channel 接收数据。 首先,我只是使用了webview小部件,但我想走得更远,并使用Thingspeak Java api自己处理
可以找到 list 文件here . 可以找到DeviceAdminReceiver类here agent_device_xml 定义如下:
01-05 18:35:42.754: E/AndroidRuntime(5814): java.lang.RuntimeException: 无法实例化 Activity ComponentInfo
我是一名优秀的程序员,十分优秀!