-1

I'm quite new to Android and I've recently started a new project that has to deal with files (to store vital flexible data that would be read each time the app starts). I've looked at some ways to write/read files at Stackoverflow (for example, this or this), at Android Devs official website and so on. I've tried all of them and every time I tried a new way, my app just crashed (even when there's just a function that deals with files, which is even never called in the code). The first error thrown in Android Monitor is always as follows:

E/System: stat file error, path is /data/app/com.example.android.a6_jars-2/lib/arm64, exception is android.system.ErrnoException: stat failed: ENOENT (No such file or directory)

I tried googling this error, but no solution I've found (in the stackoverflow too) even looked like it would be best for my situation.

Any ideas on how to solve it?

If it helps. here's all of my logcat for that app:

07-29 00:32:32.518 23458-23458/? W/Zygote: mz_is_rooted false
07-29 00:32:32.521 23458-23458/? I/art: Late-enabling -Xcheck:jni
07-29 00:32:32.751 23458-23458/com.example.android.a6_jars E/System: stat file error, path is /data/app/com.example.android.a6_jars-2/lib/arm64, exception is android.system.ErrnoException: stat failed: ENOENT (No such file or directory)
07-29 00:32:32.753 23458-23458/com.example.android.a6_jars W/linker: /system/lib64/libfilterUtils.so: unused DT entry: type 0x6ffffffe arg 0x808
07-29 00:32:32.753 23458-23458/com.example.android.a6_jars W/linker: /system/lib64/libfilterUtils.so: unused DT entry: type 0x6fffffff arg 0x2
07-29 00:32:32.757 23458-23458/com.example.android.a6_jars I/InstantRun: starting instant run server: is main process
07-29 00:32:32.886 23458-23458/com.example.android.a6_jars E/ActivityThread: Exception when newActivity r=ActivityRecord{2f4ff0b5 token=android.os.BinderProxy@215ed34a {com.example.android.a6_jars/com.example.android.a6_jars.MainActivity}} token=android.os.BinderProxy@215ed34a
07-29 00:32:32.887 23458-23458/com.example.android.a6_jars E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.android.a6_jars, PID: 23458
                                                                             java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.a6_jars/com.example.android.a6_jars.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2573)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:183)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                 at android.os.Looper.loop(Looper.java:194)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5834)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1119)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                                 at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
                                                                                 at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
                                                                                 at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                                 at com.example.android.a6_jars.MainActivity.<init>(MainActivity.java:22)
                                                                                 at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                                 at java.lang.Class.newInstance(Class.java:1650)
                                                                                 at android.app.Instrumentation.newActivity(Instrumentation.java:1074)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2550)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                 at android.app.ActivityThread.access$900(ActivityThread.java:183) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                 at android.os.Looper.loop(Looper.java:194) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5834) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1119) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885) 

The code that makes troubles (without it app works normal):

2 functions (one of them is not even called, and even if neither of them is called, the log above looks the same):

public void writeToFile(String data,Context context) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        }
        catch (IOException e) {
            Log.e("Exception", "File write failed: " + e.toString());
        }
    }

    public String readFromFile(Context context) {

        String ret = "";

        try {
            InputStream inputStream = context.openFileInput("config.txt");

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

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

                inputStream.close();
                ret = stringBuilder.toString();
            }
        }
        catch (FileNotFoundException e) {
            Log.e("login activity", "File not found: " + e.toString());
        } catch (IOException e) {
            Log.e("login activity", "Can not read file: " + e.toString());
        }

        return ret;
    }

UPD

After dealing with part of code that called findViewById() before the call to setContentView() I still have problems, the logcat is as follows:

07-29 01:22:50.367 27839-27839/? W/Zygote: mz_is_rooted false
07-29 01:22:50.370 27839-27839/? I/art: Late-enabling -Xcheck:jni
07-29 01:22:50.575 27839-27839/com.example.android.a6_jars E/System: stat file error, path is /data/app/com.example.android.a6_jars-1/lib/arm64, exception is android.system.ErrnoException: stat failed: ENOENT (No such file or directory)
07-29 01:22:50.577 27839-27839/com.example.android.a6_jars W/linker: /system/lib64/libfilterUtils.so: unused DT entry: type 0x6ffffffe arg 0x808
07-29 01:22:50.577 27839-27839/com.example.android.a6_jars W/linker: /system/lib64/libfilterUtils.so: unused DT entry: type 0x6fffffff arg 0x2
07-29 01:22:50.599 27839-27839/com.example.android.a6_jars I/InstantRun: starting instant run server: is main process
07-29 01:22:50.740 27839-27839/com.example.android.a6_jars 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
07-29 01:22:51.085 27839-27993/com.example.android.a6_jars E/GED: Failed to get GED Log Buf, err(0)

                                                                  [ 07-29 01:22:51.085 27839:27993 I/         ]
                                                                  elapse(include ctx switch):6591 (ms), eglInitialize
07-29 01:22:51.085 27839-27993/com.example.android.a6_jars I/OpenGLRenderer: Initialized EGL, version 1.4
07-29 01:22:51.104 27839-27993/com.example.android.a6_jars I/OpenGLRenderer: Get enable program binary service property (1)
07-29 01:22:51.104 27839-27993/com.example.android.a6_jars I/OpenGLRenderer: Initializing program atlas...
07-29 01:22:51.105 27839-27993/com.example.android.a6_jars I/OpenGLRenderer: Program binary detail: Binary length is 169916, program map length is 152.
07-29 01:22:51.105 27839-27993/com.example.android.a6_jars I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 54, and path is /dev/ashmem.
07-29 01:22:51.105 27839-27993/com.example.android.a6_jars I/OpenGLRenderer: No need to use file discriptor anymore, close fd(54).
07-29 01:22:51.130 27839-27839/com.example.android.a6_jars W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
Oresto
  • 135
  • 2
  • 11
  • well, OK, but when using code like this: `InputStream inputStream = context.openFileInput("config.txt");` I **don't specify** any path. – Oresto Jul 28 '17 at 22:48
  • 1
    When your app crashes, there will be [a Java stack trace in LogCat](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) (not just this error message). If you do not understand the stack trace, your question here should be a [mcve], which would include the entire stack trace along with your code that is referenced in that stack trace. – CommonsWare Jul 28 '17 at 22:50
  • Pasted stack trace and problematic piece of code, hope it will easen understanding of my problem – Oresto Jul 28 '17 at 23:08
  • 1
    When you edit a question, if you want a previous commenter to review the changes, use @ notation (akin to Twitter) in your comment to put your comment in the other person's inbox. – CommonsWare Jul 28 '17 at 23:12
  • @CommonsWare, OK, is my problem more clear now? – Oresto Jul 28 '17 at 23:18
  • @Oresto please check output on logcat Log.d("login activity", "check debug output"); .I think your first error "E/System: stat file error, path is /data/app/com.example" not output for function Log.i() , Log.d(). I think its bug on after update to Firmware:Flyme 6.1.0.0G , Meizu(m2/m2 note/m3 note/ maybee other). – Fortran Nov 02 '17 at 12:30

1 Answers1

0

The three key lines here are:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

and:

at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.android.a6_jars.MainActivity.<init>(MainActivity.java:22)

Here, :22 refers the line number in your MainActivity. There, you are calling findViewById(), and that is triggering your crash.

Your code in your question does not show this line.

In this case, even without seeing your code, I can tell that you are doing something like this in line 22, where you declare a field and try to initialize it:

TextView something=(TextView)findViewById(R.id.something);

This will not work. You cannot call findViewById() until after the call to setContentView() or something else that causes this widget to exist.

The book you read on Android app development, or the course that you took on Android app development, should show this being done over two lines. You can still have the field:

TextView something;

However, you cannot initialize it until after setContentView(), resulting in something like this:

public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.activity_main);
  something=(TextView)findViewById(R.id.something);

  // other cool stuff goes here
}

For example, in this sample project, I have:

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class CheckBoxDemo extends Activity implements
    CompoundButton.OnCheckedChangeListener {
  CheckBox cb;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    cb=(CheckBox)findViewById(R.id.check);
    cb.setOnCheckedChangeListener(this);
  }

  public void onCheckedChanged(CompoundButton buttonView,
                               boolean isChecked) {
    if (isChecked) {
      cb.setText(R.string.checked);
    }
    else {
      cb.setText(R.string.unchecked);
    }
  }
}

My field (CheckBox cb;) is not initialized until after setContentView().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491