How to capture the cause of an exception from a throwable? - android

I have the following code to track uncaught exceptions using google analytics:
ExceptionReporter myHandler =
new ExceptionReporter(AnalyticsTrackers.getInstance().get(AnalyticsTrackers.Target.APP),
Thread.getDefaultUncaughtExceptionHandler(), this);
ExceptionParser ep = new ExceptionParser() {
#Override
public String getDescription(String s, Throwable throwable) {
return "UserID:" + userID + " Version:" + versionName + " Exception:" + stringifyStacktrace(null, throwable);
}
};
private String stringifyStacktrace (Exception e, Throwable t){
if(e != null) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
return sw.toString();
} else {
return t.toString() + Arrays.toString(t.getStackTrace());
}
}
The problem is I am getting only the following:
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.jiyofit.basic_app/in.jiyofit.basic_app.WorkoutActivity}: java.lang.ArithmeticException: divide by zero
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I want the rest of the stacktrace that gives the line cause for the exception. I am not getting the following part of the stacktrace:
Caused by: java.lang.ArithmeticException: divide by zero
at in.jiyofit.basic_app.WorkoutActivity.onCreate(WorkoutActivity.java:79)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method)
How to get this also?

Try out the static method getStackTraceString() from the Log class

Related

How can I sort out the error of null pointer occurring at run method in tflte

I want to predict hand written words using pre-trained model which is trained using CNN and RNN Combination. offline handwritten words recognizer without using server.
public void pickImage(View view) {
bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.img1);
img.setImageBitmap(bitmap);
Toast.makeText(getApplicationContext(),"Width:- "+bitmap.getWidth()+" Height "+bitmap.getHeight(),Toast.LENGTH_SHORT).show();
convertBitmapToByte();
}
private void convertBitmapToByte()
{
if (mdata == null)
{
Toast.makeText(getApplicationContext(),"Return",Toast.LENGTH_SHORT).show();
return;
}
mdata.rewind();
bitmap.getPixels(getPixels,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
int pixel =0;
for(int i=0;i<748;++i)
{
for(int j=0;j<294;++j)
{
final int val = getPixels[pixel++];
mdata.putFloat(convertToGreyScale(val));
}
}
}
private float convertToGreyScale(int colour)
{
return (((colour >> 16) & 0xFF)+((colour >>8) & 0xFF)+((colour & 0xFF)))/3.0f/255.0f;
}
public void predAndSet(View view) {
tv.setText("Temp");
if(mdata == null)
{
Toast.makeText(getApplicationContext(),"Return",Toast.LENGTH_SHORT).show();
return;
}
interpreter.run(mdata,mResult);
tv.setText(Arrays.toString(mResult[0]));
}
}
Stacktrace
Process: com.soham.s.charrecongizer, PID: 29211
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6669)
at android.view.View.performClickInternal(View.java:6638)
at android.view.View.access$3100(View.java:789)
at android.view.View$PerformClick.run(View.java:26145)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6863)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6669) 
at android.view.View.performClickInternal(View.java:6638) 
at android.view.View.access$3100(View.java:789) 
at android.view.View$PerformClick.run(View.java:26145) 
at android.os.Handler.handleCallback(Handler.java:873) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6863) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void org.tensorflow.lite.Interpreter.run(java.lang.Object, java.lang.Object)' on a null object reference
at com.soham.s.charrecongizer.MainActivity.predAndSet(MainActivity.java:103)
I want to see the text in the image to be predicted and to be shown in a textbox

Back button in array causing app to crash

so im doing a school project on a movie review app. so heres my code for the next and previous button. The next button works fine without crashing. when i press back it works but once it reaches the very first movie and i press back again the app crashes.
private Movie movies[]= new Movie[5];
public MovieCollection(){
prepareMovies();
}
public Movie searchById(String id){
Movie movie = null;
for(int index=0; index < movies.length; index++){
movie = movies[index];
if(movie.getId().equals(id)){
return movie;
}
}
return movie;
}
public Movie getNextMovie(String currentMovieId){
Movie movie = null;
for(int index = 0; index < movies.length; index++) {
String tempMovieId = movies[index].getId();
if (tempMovieId.equals(currentMovieId) && (index < movies.length - 1)) {
movie = movies[index + 1];
break;
}
}
return movie;
}
public Movie getPrevMovie(String currentMovieId){
Movie movie = null;
for(int index = 0; index < movies.length; index++){
String tempMovieId = movies[index].getId();
if(tempMovieId.equals(currentMovieId) && (index > 0)){
movie = movies[index - 1];
break;
}
}
return movie;
}
my code for the button.
public void goNext(View view){
Movie nextMovie = movieCollection.getNextMovie(movieId);
if(nextMovie != null){
movieId = nextMovie.getId();
title = nextMovie.getTitle();
plot = nextMovie.getPlot();
movieArt = nextMovie.getMovieArt();
rating = nextMovie.getRating();
review1 = nextMovie.getReview1();
review2 = nextMovie.getReview2();
displayMovie(title , plot, movieArt, rating, review1, review2);
}
}
public void goPrevious(View view){
Movie prevMovie = movieCollection.getPrevMovie(movieId);
if (prevMovie != null){
movieId = prevMovie.getId();
title = prevMovie.getTitle();
plot = prevMovie.getPlot();
movieArt = prevMovie.getMovieArt();
rating = prevMovie.getRating();
review1 = prevMovie.getReview1();
review2 = prevMovie.getReview2();
displayMovie(title , plot, movieArt, rating, review1, review2);
}
}
error report can anyone tell me what is wrong with my code. thanks
07-06 14:19:36.635 3782-3782/com.example.growt.lucascomt E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.growt.lucascomt, PID: 3782
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.growt.lucascomt.Movie.getId()' on a null object reference
at com.example.growt.lucascomt.MovieCollection.getPrevMovie(MovieCollection.java:38)
at com.example.growt.lucascomt.MovieInfo.goPrevious(MovieInfo.java:86)
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Why cannot convert from string to int?

It should be something easy to do, but these commands return error when I try to convert a String to integer:
Intent recibir = getIntent();
String nombre = recibir.getStringExtra("hora_inicio");
int numero=0;
try {
numero = Integer.parseInt(nombre);
} catch(NumberFormatException nfe) {
nfe.printStackTrace();
}
Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();
Android monitor returns this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cristobal.policlinica, PID: 11025
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.cristobal.policlinica/com.example.cristobal.policlinica.CalendarActivity}:
android.content.res.Resources$NotFoundException: String resource ID
0x9
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.res.Resources$NotFoundException: String
resource 0x9
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.Toast.makeText(Toast.java:286)
at
com.example.cristobal.policlinica.CalendarActivity.onCreate(CalendarActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6237)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
thanks in advance
The problem is not with your conversion but with your Toast.makeText. When you call Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();
It considers numero as resId and tries to find the relative string resource in strings.xml which fails giving error:
Resources$NotFoundException: String resource ID
Instead of
Toast.makeText(this, numero, Toast.LENGTH_SHORT).show();
DO
Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();
Try this:
number = Integer.parseInt(YourStringName.toString());
Toast.makeText(this, String.valueOf(numero), Toast.LENGTH_SHORT).show();

Index 0 requested, with a size of 0 MyHelper Java Error

I know this problem so many times asked. and i read every post that made by others here but i cant really solve this problem on my case.
Please help me why this happen and how to fix it ?
this is my code :
public StoreData getStore(String storeid) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where "
+ COL_STORE_ID + "=?", new String[]{storeid});
StoreData data;
System.out.println("This is the problem");
c.moveToFirst();
data = new StoreData(
c.getString(c.getColumnIndex(COL_STORE_ID)),
c.getString(c.getColumnIndex(COL_ACCOUNT_ID)),
c.getString(c.getColumnIndex(COL_KOTA_ID)),
c.getString(c.getColumnIndex(COL_STORE_NAME)),
c.getString(c.getColumnIndex(COL_STORE_MANAGER)),
c.getString(c.getColumnIndex(COL_STORE_ADDRESS)),
c.getString(c.getColumnIndex(COL_STORE_TELEPHONE)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LAT)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LONG)),
c.getString(c.getColumnIndex(COL_STORE_LEADTIME)),
c.getString(c.getColumnIndex(COL_STORE_MD)));
db.close();
c.close();
return data;
}
this is my log :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.userinarts.mobilemerchpre/com.apps.userinarts.MobileMerchPre.StoresCheckinActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.apps.userinarts.MobileMerchPre.db.StoreHelper.getStore(StoreHelper.java:130)
at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.inCaseOffline(StoresCheckinActivity.java:90)
at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.onCreate(StoresCheckinActivity.java:83)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
looks like your query returned 0 results. To check it, don't ignore result of moveToFirst(). It will return false if cursor size is 0.
Do something like this:
if(c.moveToFirst()) {
data = new StoreData(
c.getString(c.getColumnIndex(COL_STORE_ID)),
c.getString(c.getColumnIndex(COL_ACCOUNT_ID)),
c.getString(c.getColumnIndex(COL_KOTA_ID)),
c.getString(c.getColumnIndex(COL_STORE_NAME)),
c.getString(c.getColumnIndex(COL_STORE_MANAGER)),
c.getString(c.getColumnIndex(COL_STORE_ADDRESS)),
c.getString(c.getColumnIndex(COL_STORE_TELEPHONE)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LAT)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LONG)),
c.getString(c.getColumnIndex(COL_STORE_LEADTIME)),
c.getString(c.getColumnIndex(COL_STORE_MD)));
db.close();
c.close();
}
return data;

How to read JSON from a URL in Android using gson [duplicate]

This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 7 years ago.
I'm trying to learn how to read and send json mensages in android.
I tried to follow the 2º answer from: "simplest way to read json from a URL in java", but for some reason it failed at the connection.
Here is the code i used:
String sURL = "http://freegeoip.net/json/";
URL url = null;
try {
url = new URL(sURL);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection request = null;
try {
request = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
request.connect();
} catch (IOException e) {
e.printStackTrace();
}
JsonParser jp = new JsonParser();
JsonElement root = null;
try {
root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
} catch (IOException e) {
e.printStackTrace();
}
JsonObject rootobj = root.getAsJsonObject();
content.setText(rootobj.get("zip_code").getAsString());
and here are the errors i got:
10-23 08:50:36.120 12366-12366/abff.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: abff.myapplication, PID: 12366
java.lang.RuntimeException: Unable to start activity ComponentInfo{abff.myapplication/abff.myapplication.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:188)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:157)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:100)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:357)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:340)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at abff.myapplication.MainActivity.onCreate(MainActivity.java:65)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
If you read the error you will see it says: android.os.NetworkOnMainThreadException. You can't perform http requests on the main thread. Make sure you use another thread or an AsyncTask

Categories

Resources