John Solly John Solly - 2 months ago 19
Android Question

App crashing when switching activities with crash report

When I switch between two of my activities it works fine they switch with no problem. But after switching a few times it doesn't work and the app crashes.Even after I close and open the application it still doesn't work. What is wrong with my application.Here is the Main activity file:

package com.example.navjeevenmann.mytycoon;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import static com.example.navjeevenmann.mytycoon.R.id.textView;


public class MainActivity extends AppCompatActivity {
private ImageButton myButton;
private int Counter;
private ImageButton autoclick;
private TextView myTextView;
Handler handler = new Handler();
private int add;
private TextView myAddView;
private ImageButton singleclick;
private int Singleadd = 1;
private ImageButton money;
private boolean[] array;

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
Counter = readCountFromFile();
add = readAddFromFile();
Singleadd=readSingleFromFile();
if(Singleadd==0){
Singleadd=1;
}

if (bundle != null) {
if (bundle.containsKey("Count")) {
Counter = bundle.getInt("Count");
}
if (bundle.containsKey("Add")) {
add = bundle.getInt("Add");
}
if (bundle.containsKey("Single")) {
Singleadd = bundle.getInt("Single");
}

}


money = (ImageButton) findViewById(R.id.imageButton2);
myButton = (ImageButton) findViewById(R.id.button);
autoclick = (ImageButton) findViewById(R.id.autoclick);
singleclick = (ImageButton) findViewById(R.id.singleclick);
myTextView = (TextView) findViewById(textView);
myAddView = (TextView) findViewById(R.id.textView2);
myButton.setBackgroundColor(0);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Counter = ButtonCounter(Counter);
}
});
autoclick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Count", Counter);
intent.putExtra("Add", add);
intent.putExtra("Single", Singleadd);

startActivity(intent);
}

});

singleclick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), ThirdActivity.class);
intent.putExtra("Count", Counter);
intent.putExtra("Single", Singleadd);
intent.putExtra("Add", add);
startActivity(intent);
}
});

}

public void onStop() {
super.onStop();
writeCounttofile();
writeAddtofile();
writeSingletofile();
}


public void onResume() {
super.onResume();

handler.postDelayed(new Runnable() {
@Override
public void run() {
Counter = AutoCounter(Counter, add);
handler.postDelayed(this, 1000);
}
}, 1000);

handler.postDelayed(new Runnable() {
@Override
public void run() {
Display(Counter, add);
handler.postDelayed(this, 1);
}
}, 50);
}


public int ButtonCounter(int Counter) {
Counter += Singleadd;
return Counter;
}

public int AutoCounter(int Counter, int add) {
Counter = Counter + add;
return Counter;
}

public void Display(int Counter, int add) {
String money = String.valueOf(Counter);
myTextView.setText(":" + money);
myAddView.setText("$" + add + "/Per Second");

}

public void writeCounttofile() {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("countvalue.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(String.valueOf(Counter));
outputStreamWriter.close();
} catch (IOException e) {
Log.v("MyActivity", e.toString());
}
}


private int readCountFromFile() {

String result = "";
int countervalue = 0;
try {

InputStream inputStream = openFileInput("countvalue.txt");

if (inputStream != null) {

InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String tempString = "";
StringBuilder stringBuilder = new StringBuilder();

while ((tempString = bufferedReader.readLine()) != null) {

stringBuilder.append(tempString);

}

inputStream.close();

result = stringBuilder.toString();
countervalue = Integer.parseInt(result);

}


} catch (FileNotFoundException e) {
Log.v("MyActivity", "File not found" + e.toString());
} catch (IOException e) {
e.printStackTrace();
}
return countervalue;
}

public void writeAddtofile() {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("addvalue.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(String.valueOf(add));
outputStreamWriter.close();
} catch (IOException e) {
Log.v("MyActivity", e.toString());
}
}


private int readAddFromFile() {

String result = "";
int add = 0;
try {

InputStream inputStream = openFileInput("addvalue.txt");

if (inputStream != null) {

InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String tempString = "";
StringBuilder stringBuilder = new StringBuilder();

while ((tempString = bufferedReader.readLine()) != null) {

stringBuilder.append(tempString);

}

inputStream.close();

result = stringBuilder.toString();
add = Integer.parseInt(result);

}


} catch (FileNotFoundException e) {
Log.v("MyActivity", "File not found" + e.toString());
} catch (IOException e) {
e.printStackTrace();
}
return add;
}

public void writeSingletofile() {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("singlevalue.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(String.valueOf(singleclick));
outputStreamWriter.close();
} catch (IOException e) {
Log.v("MyActivity", e.toString());
}
}


private int readSingleFromFile() {

String result = "";
int single = 1;
try {

InputStream inputStream = openFileInput("singlevalue.txt");

if (inputStream != null) {

InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

String tempString = "";
StringBuilder stringBuilder = new StringBuilder();

while ((tempString = bufferedReader.readLine()) != null) {

stringBuilder.append(tempString);

}

inputStream.close();

result = stringBuilder.toString();
single = Integer.parseInt(result);

}


} catch (FileNotFoundException e) {
Log.v("MyActivity", "File not found" + e.toString());
} catch (IOException e) {
e.printStackTrace();
}
return single;
}


}


Crash Log:

08-17 23:57:07.124 21715-21715/? I/art: Late-enabling -Xcheck:jni
08-17 23:57:07.226 21715-21715/com.example.navjeevenmann.mytycoon
W/System: ClassLoader referenced unknown path:/data/app/com.example.navjeevenmann.mytycoon-1/lib/arm64
08-17 23:57:07.236 21715-21715/com.example.navjeevenmann.mytycoon I/InstantRun: starting instant run server: is main process
08-17 23:57:07.287 21715-21715/com.example.navjeevenmann.mytycoon W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-17 23:57:07.308 21715-21715/com.example.navjeevenmann.mytycoon V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
08-17 23:57:07.308 21715-21715/com.example.navjeevenmann.mytycoon V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
08-17 23:57:07.308 21715-21715/com.example.navjeevenmann.mytycoon V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
08-17 23:57:07.308 21715-21715/com.example.navjeevenmann.mytycoon V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
08-17 23:57:07.308 21715-21715/com.example.navjeevenmann.mytycoon V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
08-17 23:57:07.310 21715-21715/com.example.navjeevenmann.mytycoon V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@ca25221
08-17 23:57:07.310 21715-21715/com.example.navjeevenmann.mytycoon V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@ca93646
08-17 23:57:07.403 21715-21715/com.example.navjeevenmann.mytycoon D/AndroidRuntime: Shutting down VM
08-17 23:57:07.404 21715-21715/com.example.navjeevenmann.mytycoon E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.navjeevenmann.mytycoon, PID: 21715 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navjeevenmann.mytycoon/com.example.navjeevenmann.mytycoon.MainActivity}: java.lang.NumberFormatException: For input string: "android.widget.ImageButton{8f1bae8 VFED..C.. ......ID 97,1733-209,1845 #7f0d0081 app:id/singleclick}"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NumberFormatException: For input string: "android.widget.ImageButton{8f1bae8 VFED..C.. ......ID 97,1733-209,1845 #7f0d0081 app:id/singleclick}"
at java.lang.Integer.parseInt(Integer.java:521)
at java.lang.Integer.parseInt(Integer.java:556)
at com.example.navjeevenmann.mytycoon.MainActivity.readSingleFromFile(MainActivity.java:282)
at com.example.navjeevenmann.mytycoon.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:6760)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2681)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6290) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
08-17 23:57:07.404 21715-21715/com.example.navjeevenmann.mytycoon D/AppTracker: App Event: crash
08-17 23:57:07.412 21715-21715/com.example.navjeevenmann.mytycoon I/Process: Sending signal. PID: 21715 SIG: 9


Please & Thank You.

Answer Source

Well your error is in MainActivity inside readCountFromFile() method.

The problem is here

        result = stringBuilder.toString();
        single = Integer.parseInt(result);

Since result can be empty or something

Integer.parseInt(result);

can cause NumberFormatException

So just try to catch it

private  int readSingleFromFile() {

String result = "";
int single = 1;
try {

    InputStream inputStream = openFileInput("singlevalue.txt");

    if (inputStream != null) {
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String tempString = "";
        StringBuilder stringBuilder = new StringBuilder();

        while ((tempString = bufferedReader.readLine()) != null) {
            stringBuilder.append(tempString);
        }

        inputStream.close();

        result = stringBuilder.toString();
        single = Integer.parseInt(result);

    }


} catch (FileNotFoundException e) {
    Log.v("MyActivity", "File not found" + e.toString());
} catch (IOException e) {
    e.printStackTrace();
} catch (NumberFormatException() {
  //here you catch and watch the problem
  Log.e("MyActivity", "cant parse string: "+result); 
}
return single;
}