There is an error "unfortunately, my application has stopped" when i run my program in android studio -
i got error when run program in android studio. message error below :
10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: fatal exception: main 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: process: com.example.natasya.myapplication, pid: 3478 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: java.lang.runtimeexception: unable start activity componentinfo{com.example.natasya.myapplication/com.example.natasya.myapplication.mainactivity}: java.lang.illegalargumentexception: invalid provider: null 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:2416) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.-wrap11(activitythread.java) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1344) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.os.looper.loop(looper.java:148) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.main(activitythread.java:5417) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ java.lang.reflect.method.invoke(native method) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: caused by: java.lang.illegalargumentexception: invalid provider: null 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.location.locationmanager.checkprovider(locationmanager.java:1704) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.location.locationmanager.getlastknownlocation(locationmanager.java:1194) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ com.example.natasya.myapplication.mainactivity.getlongilatifile(mainactivity.java:103) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ com.example.natasya.myapplication.mainactivity.oncreate(mainactivity.java:76) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activity.performcreate(activity.java:6237) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:2369) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.-wrap11(activitythread.java) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1344) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.os.looper.loop(looper.java:148) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ android.app.activitythread.main(activitythread.java:5417) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ java.lang.reflect.method.invoke(native method) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) 10-22 19:13:54.831 3478-3478/com.example.natasya.myapplication e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616)
line 103 of mainactivity.java follows:
location location = locationmanager.getlastknownlocation(bestprovider);
and permission code in androidmanifest.xml :
<uses-permission android:name="android.permission.write_external_storage" ></uses-permission> <uses-permission android:name="android.permission.record_audio" ></uses-permission> <uses-permission android:name="android.permission.access_coarse_location" ></uses-permission> <uses-permission android:name="android.permission.access_fine_location" ></uses-permission> <uses-permission android:name="android.permission.internet" ></uses-permission> <uses-permission android:name="android.permission.read_external_storage" ></uses-permission>
i confused why happening? please me.
this mainactivity :
import java.io.file; import java.io.ioexception; import java.sql.sqloutput; import java.util.arraylist; import java.util.date; import android.app.activity; import android.content.context; import android.content.intent; import android.location.criteria; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.media.mediarecorder; import android.os.build; import android.os.bundle; import android.os.environment; import android.os.strictmode; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.toast; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.responsehandler; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.basicresponsehandler; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import java.text.simpledateformat; import java.util.list; public class mainactivity extends activity { public double longi1 = 0.0; public double lati1 = 0.0; public locationlistener locationlistener ; public textview longi1; public textview lati1; button b1; int cekbutton; public string filename=""; simpledateformat formatter = new simpledateformat("yyyy_mm_dd_hh_mm_ss"); date = new date(); string filename = formatter.format(now); private static final string audio_recorder_file_ext_amr = ".amr"; private static final string audio_recorder_folder = "audiorecorder"; private mediarecorder recorder = null; private int currentformat = 0; private int output_formats[] = {mediarecorder.outputformat.raw_amr}; private string file_exts[] = {audio_recorder_file_ext_amr}; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if(build.version.sdk_int > 9){ strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); } b1 = (button) findviewbyid(r.id.button); getlongilatifile(); b1.setonclicklistener(new view.onclicklistener(){ public void onclick(view v){ if(cekbutton==0) { cekbutton=1; b1.settext("stop"); startrecording(); } else if(cekbutton==1) { stoprecording(); intent = new intent(mainactivity.this,halaman2.class); i.putextra("nama", filename); startactivity(i); } } }); } public void getlongilatifile(){ locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service); criteria criteria = new criteria(); string bestprovider = locationmanager.getbestprovider(criteria, true); location location = locationmanager.getlastknownlocation(bestprovider); longi1= (textview) findviewbyid(r.id.textview1); lati1= (textview) findviewbyid(r.id.textview); locationmanager.requestlocationupdates(locationmanager.gps_provider, 1, 1, new locationlistener() { @override public void onstatuschanged(string provider, int status, bundle extras) { // todo auto-generated method stub } @override public void onproviderenabled(string provider) { // todo auto-generated method stub } @override public void onproviderdisabled(string provider) { // todo auto-generated method stub } @override public void onlocationchanged(location location) { // todo auto-generated method stub longi1 = location.getlongitude(); lati1 = location.getlatitude(); longi1.settext(longi1+""); lati1.settext(lati1+""); try { defaulthttpclient hc=new defaulthttpclient(); responsehandler<string> res=new basicresponsehandler(); httppost postmethod = new httppost("http://10.0.2.2:8080/send_malem_3.php"); system.out.printf("test setelah nyambung"); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("nama", filename+".amr")); postmethod.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response2 = hc.execute(postmethod); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { toast.maketext(getbasecontext(), "gagal 2", toast.length_short).show(); // todo auto-generated catch block e.printstacktrace(); } } }); } private void startrecording(){ recorder = new mediarecorder(); recorder.setaudiosource(mediarecorder.audiosource.mic); recorder.setoutputformat(output_formats[currentformat]); recorder.setaudioencoder(mediarecorder.audioencoder.amr_nb); getfilename(); recorder.setoutputfile(filename); recorder.setonerrorlistener(errorlistener); recorder.setoninfolistener(infolistener); try { recorder.prepare(); recorder.start(); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } private void getfilename(){ string filepath = environment.getexternalstoragedirectory().getpath(); file file = new file(filepath,audio_recorder_folder); if(!file.exists()){ file.mkdirs(); } filename = (file.getabsolutepath() + "/" + filename + file_exts[currentformat]); } private mediarecorder.onerrorlistener errorlistener = new mediarecorder.onerrorlistener() { @override public void onerror(mediarecorder mr, int what, int extra) { applog.logstring("error: " + + ", " + extra); } }; private mediarecorder.oninfolistener infolistener = new mediarecorder.oninfolistener() { @override public void oninfo(mediarecorder mr, int what, int extra) { applog.logstring("warning: " + + ", " + extra); } }; private void stoprecording(){ if(null != recorder){ recorder.stop(); recorder.reset(); recorder.release(); recorder = null; } }
}
thanks, agung
the provider passed getlastknownlocation
should 1 of following string constants:
gps_provider
, has value "gps";network_provider
, has value "network";passive_provider
, has value "passive";
here source code of getlastknownlocation
:
public location getlastknownlocation(string provider) { checkprovider(provider); string packagename = mcontext.getpackagename(); locationrequest request = locationrequest.createfromdeprecatedprovider( provider, 0, 0, true); try { return mservice.getlastlocation(request, packagename); } catch (remoteexception e) { log.e(tag, "remoteexception", e); return null; } }
and method checkprovider
:
private static void checkprovider(string provider) { if (provider == null) { throw new illegalargumentexception("invalid provider: " + provider); } }
from can conclude argument bestprovider
null, rather valid provider string.
Comments
Post a Comment