Android Java.Lang.RuntimeException : Unable to start activity Component Info -
i'm new in android programmer. recently, trying make project using google map v2 in android app, i'm getting lot of error (i have add google play services library , find similar topic in here, not solved problem).please see log cat bellow :
updated
03-22 13:52:29.331: e/androidruntime(17708): fatal exception: main 03-22 13:52:29.331: e/androidruntime(17708): java.lang.runtimeexception: unable resume activity {com.yai.testmap/com.yai.testmap.mainactivity}: java.lang.nullpointerexception 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activitythread.performresumeactivity(activitythread.java:2613) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activitythread.handleresumeactivity(activitythread.java:2641) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2127) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activitythread.access$600(activitythread.java:140) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activitythread$h.handlemessage(activitythread.java:1228) 03-22 13:52:29.331: e/androidruntime(17708): @ android.os.handler.dispatchmessage(handler.java:99) 03-22 13:52:29.331: e/androidruntime(17708): @ android.os.looper.loop(looper.java:137) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activitythread.main(activitythread.java:4895) 03-22 13:52:29.331: e/androidruntime(17708): @ java.lang.reflect.method.invokenative(native method) 03-22 13:52:29.331: e/androidruntime(17708): @ java.lang.reflect.method.invoke(method.java:511) 03-22 13:52:29.331: e/androidruntime(17708): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:994) 03-22 13:52:29.331: e/androidruntime(17708): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:761) 03-22 13:52:29.331: e/androidruntime(17708): @ dalvik.system.nativestart.main(native method) 03-22 13:52:29.331: e/androidruntime(17708): caused by: java.lang.nullpointerexception 03-22 13:52:29.331: e/androidruntime(17708): @ com.yai.testmap.mainactivity.setupmapifneeded(mainactivity.java:93) 03-22 13:52:29.331: e/androidruntime(17708): @ com.yai.testmap.mainactivity.onresume(mainactivity.java:187) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.instrumentation.callactivityonresume(instrumentation.java:1199) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activity.performresume(activity.java:5237) 03-22 13:52:29.331: e/androidruntime(17708): @ android.app.activitythread.performresumeactivity(activitythread.java:2603) 03-22 13:52:29.331: e/androidruntime(17708): ... 12 more
update
my mainactivity.java :
package com.yai.testmap; import android.support.v7.app.actionbaractivity; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; /* error repport */ import android.util.log; /* import dropdownlist used project */ import android.widget.checkbox; import android.widget.spinner; import android.widget.toast; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.adapterview.onitemselectedlistener; /* import google map */ import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; /* google map type */ import static com.google.android.gms.maps.googlemap.map_type_hybrid; import static com.google.android.gms.maps.googlemap.map_type_none; import static com.google.android.gms.maps.googlemap.map_type_normal; import static com.google.android.gms.maps.googlemap.map_type_satellite; import static com.google.android.gms.maps.googlemap.map_type_terrain; class placeholderfragment extends fragment implements onitemselectedlistener{ private googlemap mgooglemap; private checkbox mtrafficcheckbox, mlocationcheckbox, mbuildingcheckbox, mindoorcheckbox; public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); spinner spinner = (spinner) rootview.findviewbyid(r.id.spinneroption); arrayadapter adapter = arrayadapter.createfromresource(getactivity(), r.array.spinner_option_string, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner.setadapter(adapter); spinner.setonitemselectedlistener((onitemselectedlistener) getactivity()); mtrafficcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_traffic); mlocationcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_location); mbuildingcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_building); mindoorcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_indoor); return rootview; } @override public void onitemselected(adapterview parent, view view, int position, long id) { // todo auto-generated method stub } @override public void onnothingselected(adapterview parent) { // todo auto-generated method stub } } public class mainactivity extends actionbaractivity implements onitemselectedlistener { private googlemap mgooglemap; private checkbox mtrafficcheckbox, mlocationcheckbox, mbuildingcheckbox, mindoorcheckbox; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } } private void setupmapifneeded(){ if(mgooglemap == null){ mgooglemap = ((supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.map)).getmap(); } } private boolean checkready(){ if(mgooglemap == null){ toast.maketext(this, r.string.map_not_ready, toast.length_short).show(); return false; } return true; } private void updatetraffic(){ if(!checkready()){ return; } mgooglemap.settrafficenabled(mtrafficcheckbox.ischecked()); } public void ontraffictoggled(view view){ updatetraffic(); } private void updatelocation(){ if(!checkready()){ return; } mgooglemap.setmylocationenabled(mlocationcheckbox.ischecked()); } public void onlocationtoggled(view view){ updatelocation(); } private void updatebuilding(){ if(!checkready()){ return; } mgooglemap.setbuildingsenabled(mbuildingcheckbox.ischecked()); } public void onbuildingtoggled(view view){ updatebuilding(); } private void updateindoor(){ if(!checkready()){ return; } mgooglemap.setindoorenabled(mindoorcheckbox.ischecked()); } public void onindoortoggled(view view){ updateindoor(); } @override public void onitemselected(adapterview parent, view view, int position, long id){ setlayer((string) parent.getitematposition(position)); } private void setlayer(string layername){ if(!checkready()){ return; } if(layername.equals(getstring(r.string.normal))){ mgooglemap.setmaptype(map_type_normal); } else if(layername.equals(getstring(r.string.hybrid))){ mgooglemap.setmaptype(map_type_hybrid); } else if(layername.equals(getstring(r.string.satellite))){ mgooglemap.setmaptype(map_type_satellite); } else if(layername.equals(getstring(r.string.terrain))){ mgooglemap.setmaptype(map_type_terrain); } else if(layername.equals(getstring(r.string.none_map))){ mgooglemap.setmaptype(map_type_none); } else{ log.i("lda", "error setting layer name " + layername); } } @override public void onnothingselected(adapterview parent){ } @override protected void onresume() { super.onresume(); setupmapifneeded(); if (mgooglemap != null) { updatetraffic(); updatelocation(); updatebuilding(); updateindoor(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } }
i'm using fragment here, here fragment_main.xml :
<?xml version="1.0" encoding="utf-8" ?>
<spinner android:id="@+id/spinneroption" android:layout_width="match_parent" android:layout_height="wrap_content" /> <checkbox android:id="@+id/checkbox_traffic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="ontraffictoggled" android:text="@string/traffic" /> <checkbox android:id="@+id/checkbox_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="onlocationtoggled" android:text="@string/location" /> <checkbox android:id="@+id/checkbox_building" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="onbuildingstoggled" android:checked="true" android:text="@string/building" /> <checkbox android:id="@+id/checkbox_indoor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="onindoortoggled" android:checked="true" android:text="@string/indoor" /> </linearlayout> </relativelayout>
and manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yai.testmap" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="9" android:targetsdkversion="17" /> <uses-permission android:name="android.permission.internet" /> <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.access_coarse_location" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-feature android:glesversion="0x00020000" android:required="true" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.yai.testmap.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="super screet code" /> </application> </manifest>
here activity_main.xml :
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.yai.testmap.mainactivity" tools:ignore="mergerootframe" />
i'm curious spinner , function setupmapifneeded(), because when i'm commenting parts of code, app work. miss? idea? many thanks.
try this..
remove spinner
, checkbox
mainactivity
placeholderfragment
below
public static class placeholderfragment extends fragment implements onitemselectedlistener{ public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); spinner spinner = (spinner) rootview.findviewbyid(r.id.spinneroption); arrayadapter adapter = arrayadapter.createfromresource(getactivity(), r.array.spinner_option_string, android.r.layout.simple_spinner_item); adapter.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); spinner.setadapter(adapter); spinner.setonitemselectedlistener(getactivity()); mtrafficcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_traffic); mlocationcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_location); mbuildingcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_building); mindoorcheckbox = (checkbox) rootview.findviewbyid(r.id.checkbox_indoor); return rootview; } }
and
@override protected void oncreate(bundle savedinstancestate) { /* create option dropdown list*/ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()).commit(); } }
Comments
Post a Comment