我对Android很新,我最近开始了一个新项目,必须处理文件(存储每次应用程序启动时都会读取的重要灵活数据)。我已经在Android Devs官方网站上查看过在Stackoverflow上编写/读取文件的一些方法(例如,this或this)。我已经尝试了所有这些,每次我尝试新方法时,我的应用程序都崩溃了(即使只有一个处理文件的函数,甚至从未在代码中调用过)。 Android Monitor中抛出的第一个错误总是如下:
E / System:stat文件错误,路径为/data/app/com.example.android.a6_jars-2/lib/arm64,异常是android.system.ErrnoException:stat failed:ENOENT(没有这样的文件或目录)
我试过谷歌搜索这个错误,但我找不到解决方案(在stackoverflow中),甚至看起来对我的情况最好。
关于如何解决它的任何想法?
如果有帮助的话。这是我对该应用程序的所有logcat:
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)
造成麻烦的代码(没有应用程序正常工作):
2个函数(其中一个函数甚至没有被调用,即使它们都没有调用,上面的日志看起来也一样):
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
在调用setContentView()之前处理调用findViewById()的部分代码后我还有问题,logcat如下:
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
答案 0 :(得分:0)
这里的三个关键线是:
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.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.android.a6_jars.MainActivity.<init>(MainActivity.java:22)
此处,:22
表示MainActivity
中的行号。在那里,你正在呼叫findViewById()
,这就是触发你的崩溃。
您问题中的代码未显示此行。
在这种情况下,即使没有看到你的代码,我也可以告诉你在第22行做了类似的事情,在那里你声明一个字段并尝试初始化它:
TextView something=(TextView)findViewById(R.id.something);
这不起作用。在调用findViewById()
之后的或导致此小部件存在的其他内容之前,您无法调用setContentView()
。
您在Android应用开发中阅读的书籍,或者您在Android应用开发中所采用的课程,应该表明这是通过两行完成的。您仍然可以拥有该字段:
TextView something;
但是,在setContentView()
之后才能初始化它,导致类似这样的内容:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
something=(TextView)findViewById(R.id.something);
// other cool stuff goes here
}
例如,在this sample project中,我有:
/***
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);
}
}
}
我的字段(CheckBox cb;
)在setContentView()
之后才会初始化。