I am loading a map in my app from kml files (with a different kml file on each day of the week being loaded). It was working until just recently and I'm really confused as to why I am getting this error as I haven't touched this part of my code since I got it working basically and now I am getting a XmlPullParserException: expected: /META read: HEAD (position:END_TAG #1:355 in java.io.InputStreamReader#426ba9d0)
Here is my code:
// set kml layers
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
InputStream inputStream = null;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int value = preferences.getInt("DateStarted", 1);
Log.d(TAG, String.valueOf(value));
System.out.println("In Maps start day is " + String.valueOf(value));
try {
// get different maps on different days of the week
switch(value) {
case Calendar.MONDAY:
inputStream = new URL("http://www.inf.ed.ac.uk/teaching/courses/selp/coursework/monday.kml").openStream();
break;
case Calendar.TUESDAY:
inputStream = new URL("http://www.inf.ed.ac.uk/teaching/courses/selp/coursework/tuesday.kml").openStream();
break;
case Calendar.WEDNESDAY:
inputStream = new URL("http://www.inf.ed.ac.uk/teaching/courses/selp/coursework/wednesday.kml").openStream();
break;
case Calendar.THURSDAY:
inputStream = new URL("http://www.inf.ed.ac.uk/teaching/courses/selp/coursework/thursday.kml").openStream();
break;
case Calendar.FRIDAY:
inputStream = new URL("http://www.inf.ed.ac.uk/teaching/courses/selp/coursework/thursday.kml").openStream();
break;
case Calendar.SATURDAY:
inputStream = new URL("http://www.inf.ed.ac.uk/teaching/courses/selp/coursework/saturday.kml").openStream();
break;
case Calendar.SUNDAY:
inputStream = new URL("http://www.inf.ed.ac.uk/teaching/courses/selp/coursework/sunday.kml").openStream();
break;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
try {
layer = new KmlLayer(mMap, inputStream, getApplicationContext());
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
layer.addLayerToMap();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
and the full error message that I'm getting is :
> W/System.err: org.xmlpull.v1.XmlPullParserException: expected: /META read: HEAD (position:END_TAG </HEAD>#1:355 in java.io.InputStreamReader#426ba9d0)
W/System.err: at org.kxml2.io.KXmlParser.readEndTag(KXmlParser.java:970)
W/System.err: at org.kxml2.io.KXmlParser.next(KXmlParser.java:372)
W/System.err: at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
W/System.err: at com.google.maps.android.kml.KmlParser.parseKml(KmlParser.java:90)
W/System.err: at com.google.maps.android.kml.KmlLayer.<init>(KmlLayer.java:49)
W/System.err: at com.example.veronika.grabble.MapsActivity.onMapReady(MapsActivity.java:169)
W/System.err: at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
W/System.err: at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
W/System.err: at android.os.Binder.transact(Binder.java:347)
W/System.err: at aai.a(:com.google.android.gms.DynamiteModulesB:82)
W/System.err: at maps.ad.t$5.run(Unknown Source)
W/System.err: at android.os.Handler.handleCallback(Handler.java:730)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err: at android.os.Looper.loop(Looper.java:213)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5225)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x420218b0)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.veronika.grabble.MapsActivity.onMapReady(MapsActivity.java:176)
at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:347)
at aai.a(:com.google.android.gms.DynamiteModulesB:82)
at maps.ad.t$5.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
Does anyone have an idea of how I should go about fixing this? Because I don't understand where does the null pointer exception comes from...
PS: the log.d shows that value has a value in one of the cases, and so that should put a different inputStream than null ....
Related
I am using the following code to write an onPostExecute process to file from my FileWriter.class, it was working fine but am updating my application and I have to use the Environment.getExternalStorageDirectory().getPath(); instead of this, i tried following. I know doing something slightly wrong. Any Help or direction would be helpful.
Thanks.
public class FileWriter {
public void appendData( String text) {
try {
File myFile = new File("/sdcard/latencyOP.csv");
// myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
if (myFile.createNewFile()) {
myOutWriter.append("TimeStamp,ExecutionTime,OperationType;");
}
myOutWriter.append(text);
myOutWriter.close();
fOut.close();
/**
* Toast.makeText(getBaseContext(),
* "Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
*/
} catch (Exception e) {
/**
* Toast.makeText(getBaseContext(), e.getMessage(),
* Toast.LENGTH_SHORT).show();
*/
}
}
}
EDIT
I forgot to add that I am attempting to do some background operations and publish results on the **FILE.csv** thread without having to manipulate threads.
UPDATED CODE #Leonardo Acevedo
try {
File myFile = new File(
Environment.getExternalStorageDirectory(),
"latencyOP.csv"
);
boolean needToCreateFile = !myFile.exists();
if (needToCreateFile) {
myFile.createNewFile();
}
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
// At this point your file is guaranteed to exist
if (needToCreateFile) {
myOutWriter.append("TimeStamp,ExecutionTime,OperationType;");
}
myOutWriter.append(text);
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT
).show();
} catch (Exception e) {
e.printStackTrace();
displayExceptionMessage(e.getMessage());
//Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public void displayExceptionMessage(String msg) {
//Toast.makeText(this, "Some Error Occured", Toast.LENGTH_SHORT).show();
}
ASYNCH TASK PROCESS
protected void onPreExecute(){ startTime = Calendar.getInstance().getTime().getTime();
}
protected void onPostExecute(Long result) {
endTime = Calendar.getInstance().getTime().getTime();
long timeDiff = endTime - startTime;
FileWriter fileWriter = new FileWriter();
String data = endTime + "," + timeDiff + "," + "Authentication" + ";";
fileWriter.appendData(data);
}
Is this wrong ?
03-14 20:42:52.062 17192-17192/com.demo.common.recordwritertest W/System.err: java.io.IOException: Permission denied
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.io.File.createNewFile(File.java:948)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.demo.common.recordwritertest.utils.FileWriter.appendData(FileWriter.java:25)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.demo.common.recordwritertest.com.demo.common.recordwritertest.asynctasks.AuthenticationAsyncTask.onPostExecute(AuthenticationAsyncTask.java:119)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.demo.common.recordwritertest.com.demo.common.recordwritertest.asynctasks.AuthenticationAsyncTask.onPostExecute(AuthenticationAsyncTask.java:27)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:667)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.os.Looper.loop(Looper.java:154)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
03-14 20:42:52.063 17192-17192/com.demo.common.recordwritertest W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I want to create a video from multiple images using mp4parser. And I found answer from this link : https://github.com/sannies/mp4parser/issues/182
I have tried it using below code.
DataSource videoFile = new FileDataSourceImpl(new File(Environment.getExternalStorageDirectory() + File.separator + "myvideo.mp4"));
Movie sor = MovieCreator.build(videoFile);
List<Track> videoTracks = sor.getTracks();
Track referenceTrack = null;
for (Track candidate : sor.getTracks()) {
if (candidate.getSyncSamples() != null && "vide".equals(candidate.getHandler()) && candidate.getSyncSamples().length > 0) {
referenceTrack = candidate;
OneJpegPerIframe oneJpegPerIframe = null;
try {
oneJpegPerIframe = new OneJpegPerIframe("eng", arr,referenceTrack);
} catch (IOException e) {
e.printStackTrace();
}
Movie movie = new Movie();
movie.addTrack(oneJpegPerIframe);
Container out = new DefaultMp4Builder().build(movie);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + File.separator + "output.mp4"));
out.writeContainer(fos.getChannel());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
But I got error like "java.lang.RuntimeException: Number of sync samples doesn't match the number of stills (30 vs. 4)" which I have shown below :
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { (has extras) }} to activity {orafox.videomaker/orafox.videomaker.activity.MainActivity}: java.lang.RuntimeException: Number of sync samples doesn't match the number of stills (30 vs. 4)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3367)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Number of sync samples doesn't match the number of stills (30 vs. 4)
at com.googlecode.mp4parser.authoring.tracks.mjpeg.OneJpegPerIframe.<init>(OneJpegPerIframe.java:39)
at activity.MainActivity.onActivityResult(MainActivity.java:135)
at android.app.Activity.dispatchActivityResult(Activity.java:5322)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)
at android.app.ActivityThread.access$1100(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I do not want to use "ffmpeg" library.
If anybody knew how to solve it please help me. Thanks in advance.
hey guys i keep getting this error in my app when i try to get data from a server and read it as json and i am stuck because it doesnt happen all the time hear is the code
private class statusUpdateTask extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... urls) {
if(testServe("http://www.gogodis.comxa.com/getData.php")) {
HttpURLConnection conn = null;
BufferedReader reader = null;
try {
URL url = new URL(urls[0]);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
retry(0);
}
return null;
}
#Override
protected void onPostExecute(String result) {
if(isJSONValid(result)) {
try {
JSONObject results = new JSONObject(result);
JSONArray jr = results.getJSONArray("results");
JSONObject set = jr.getJSONObject(0);
JSONObject info = set.getJSONArray("info").getJSONObject(0);
if (info.getString("state").equals("ok")) {
if (info.getInt("inbox") == 1) {
inbox.setImageResource(R.drawable.camr);
} else inbox.setImageResource(R.drawable.cam);
if (info.getInt("chat") == 1) {
chat.setImageResource(R.drawable.msgr);
} else chat.setImageResource(R.drawable.msg);
if(info.getInt("match") == 1){
new newMatchUpdateTask().execute("http://www.gogodis.comxa.com/getData.php?type=new_match&&user=" + my_user_id);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
retry(0);
}
}
}
and hear is the logcast
05-30 15:05:11.205 5071-5394/com.reallyChat W/System.err: java.net.SocketTimeoutException
05-30 15:05:11.676 5071-5394/com.reallyChat W/System.err: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
05-30 15:05:11.676 5071-5394/com.reallyChat W/System.err: at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.io.InputStream.read(InputStream.java:163)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at libcore.io.Streams.readAsciiLine(Streams.java:201)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at com.really_chat.results$statusUpdateTask.doInBackground(results.java:968)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at com.really_chat.results$statusUpdateTask.doInBackground(results.java:953)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-30 15:05:11.686 5071-5394/com.reallyChat W/System.err: at java.lang.Thread.run(Thread.java:856)
05-30 15:05:11.696 5071-5071/com.reallyChat D/AndroidRuntime: Shutting down VM
05-30 15:05:11.706 5071-5071/com.reallyChat W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x411cd930)
05-30 15:05:11.726 5071-5071/com.reallyChat E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:154)
at org.json.JSONObject.<init>(JSONObject.java:171)
at com.really_chat.results.isJSONValid(results.java:1158)
at com.really_chat.results$statusUpdateTask.onPostExecute(results.java:989)
at com.really_chat.results$statusUpdateTask.onPostExecute(results.java:953)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
iv'e even tried to put in code that confirms its json but still no luck
public boolean isJSONValid(String test) {
try{
new JSONObject(test);
}catch(JSONException ex){
try{
new JSONArray(test);
}catch(JSONException ex1){
return false;
}
}
return true;
}
private boolean testServe(String url) {
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(15000);
connection.connect();
return true;
} catch (Exception e) {
// Handle your exceptions
return false;
}
}
Try to change like this
conn.setReadTimeout(0 /* milliseconds */);//Infinite timeout
conn.setConnectTimeout(25000 /* milliseconds */);
This question already has answers here:
NetworkOnMainThreadException [duplicate]
(5 answers)
Closed 7 years ago.
I'm getting NetworkOnMainThreadException while using Runnable
Code:
public class FullscreenActivity extends AppCompatActivity {
public Socket socket;
public int SERVERPORT = 5000; /* port 5000 (for testing) */
public String SERVER_IP = "10.0.2.2"; /* local Android address of localhost */
ViewFlipper flipper;
ListView listing;
public void attemptConnect(View view) {
try {
EditText editTextAddress = (EditText) findViewById(R.id.address);
EditText editTextPort = (EditText) findViewById(R.id.port);
String SERVER_IP_loc = editTextAddress.getText().toString();
int SERVERPORT_loc = Integer.parseInt(editTextPort.getText().toString());
System.out.println("Attempt Connect: IP " + SERVER_IP_loc + " Port: " + SERVERPORT_loc);
System.out.println("SET");
ClientThread myClientTask = new ClientThread(SERVER_IP_loc, SERVERPORT_loc);
myClientTask.run();
System.out.println("CONNECTED");
flipper.setDisplayedChild(1);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
// Thinks the following is not a thread ??
class ClientThread implements Runnable {
ClientThread(String addr, int port) {
SERVER_IP = addr;
SERVERPORT = port;
}
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
// gets to this line fine
socket = new Socket(serverAddr, SERVERPORT);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
My permissions are set up to allow internet access:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
I'm getting the following error:
6188-6188/test W/System.err: android.os.NetworkOnMainThreadException
6188-6188/test W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
6188-6188/test W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
6188-6188/test W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
6188-6188/test W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:122)
6188-6188/test W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
6188-6188/test W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
6188-6188/test W/System.err: at java.net.Socket.startupSocket(Socket.java:592)
6188-6188/test W/System.err: at java.net.Socket.<init>(Socket.java:226)
6188-6188/test W/System.err: at test.FullscreenActivity$ClientThread.run(FullscreenActivity.java:363)
6188-6188/test W/System.err: at test(FullscreenActivity.java:340)
6188-6188/test W/System.err: at java.lang.reflect.Method.invoke(Native Method)
6188-6188/test W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(test:270)
6188-6188/test W/System.err: at android.view.View.performClick(View.java:5198)
6188-6188/test W/System.err: at android.view.View$PerformClick.run(View.java:21147)
6188-6188/test W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
6188-6188/test W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
6188-6188/test W/System.err: at android.os.Looper.loop(Looper.java:148)
6188-6188/test W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
6188-6188/test W/System.err: at java.lang.reflect.Method.invoke(Native Method)
6188-6188/test W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
6188-6188/test W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
As far as I can tell, this should function as expected. It seems to think the Runnable isn't a thread, but I can't figure out why.
You need a Thread to run your Runnable for you. Simply calling run on a Runnable does not run it on a different thread.
new Thread(new ClientThread(SERVER_IP_loc, SERVERPORT_loc)).start();
I want my program to record audio for 10 seconds and then stop and store my record in file storage, everything works fine on my Nexus 4 and Galaxy S 5, but when i test it in Galaxy S3 it crushes and raise error
10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/AudioCaptureDemo﹕ prepare() failed
10-02 02:13:44.942 1279-1279/com.taptester.tappapp E/MediaRecorder﹕ start called in an invalid state: 4
10-02 02:13:44.942 1279-1279/com.taptester.tappapp D/AndroidRuntime﹕ Shutting down VM
10-02 02:13:44.942 1279-1279/com.taptester.tappapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1a12ba8)
10-02 02:13:44.952 1279-1279/com.taptester.tappapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.taptester.tappapp, PID: 1279
java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.taptester.tappapp.MainActivity.startRecording(MainActivity.java:203)
at com.taptester.tappapp.MainActivity.access$300(MainActivity.java:62)
at com.taptester.tappapp.MainActivity$6.onFinish(MainActivity.java:825)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
First i thought the error is in the file name, I'm declaring it like this:
public MainActivity() {
mFileName = Environment.getExternalStorageDirectory() + File.separator
+ Environment.DIRECTORY_DCIM + File.separator + "MyMemo.3gp";
//Environment.getExternalStorageDirectory().getAbsolutePath();
//mFileName += "/MyMemo.3gp";
}
Then i make a record like this:
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try {
mRecorder.prepare();
mRecorder.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
Then i call the "StartRecording" method like this:
else if(command.equals("2")) {
startRecording();
Toast.makeText(getApplicationContext(), "Start recording...",
Toast.LENGTH_SHORT).show();
CountDownTimer start = new CountDownTimer(timer, 1000) {
#Override
public void onTick(long l) {
Toast.makeText(getApplicationContext(), "Recording!!!",
Toast.LENGTH_SHORT).show();
}
#Override
public void onFinish() {
stopRecording();
}
}.start();
you are not supposed to call mRecorder.start(); when mRecorder.prepare(); fails.
this is what caused the Illegal State Exception to be thrown.
Not all devices support all the encoding formats.
try changing these mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
and mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
change the format and Encoder to a file format and encoding format your s3 device supports.
for a start, change your encoding setting to default settings as follows and try if it works.
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);