使用崩溃报告切换活动时应用程序崩溃

时间:2017-08-18 11:16:48

标签: android crash crash-reports

当我在我的两个活动之间切换时,它可以正常切换,没有任何问题。但是在切换几次之后它就不起作用而应用程序崩溃了。即使在我关闭并打开应用程序之后它仍然无法正常工作。我的申请有什么问题。这是主要的活动文件:

 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;
}


 }

崩溃日志:

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

请&谢谢。

3 个答案:

答案 0 :(得分:0)

根据此日志判断

 java.lang.NumberFormatException: For input string: 
 "android.widget.ImageButton{8f1bae8 VFED..C.. ......ID 97,1733-
 209,1845 #7f0d0081 app:id/singleclick}"

我认为您在writeSingleToFile()readSingleFromFile()时遇到问题。

writeSingleToFile中,您正在呼叫String.valueof(singleclick)。这将最终调用toString()类的ImageButton方法。然后,在readSingleFromFile()中,您试图将此值解析为int,这是失败的。

我认为您需要修改逻辑并仔细检查您要保存的内容。

答案 1 :(得分:0)

您认为从文件中读取的值始终为整数

single = Integer.parseInt(result);

但事实并非如此。因此它引发了一个例外:

java.lang.NumberFormatException: For input string: "android.widget.ImageButton{8f1bae8 VFED..C.. ......ID 97,1733-209,1845 #7f0d0081 app:id/singleclick}"

看,它尝试将此String转换为int:

  

android.widget.ImageButton {8f1bae8 VFED..C .. ...... ID 97,1733-209,1845#7f0d0081 app:id / singleclick}

您需要检查逻辑并将文件写入您想要的值。 无论如何,当您从“远程”存储中读取内容时,您需要检查它是否符合您的条件。此外,您需要开发逻辑,以便即使您从文件中读取的值也是意外的。

您可以尝试在try / catch中包装 Integer.parseInt(result); 并正确处理catch部分中的异常,例如,您可能会返回一些可用于逻辑的默认值

答案 2 :(得分:0)

你的错误是在readCountFromFile()方法内的MainActivity中。

问题出在这里

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

因为结果可能是空的或什么

Integer.parseInt(result);

会导致NumberFormatException

所以试着抓住它

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;
}