Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Filelist抛出NullPointerException_Java_Android_Android Context - Fatal编程技术网

Java Filelist抛出NullPointerException

Java Filelist抛出NullPointerException,java,android,android-context,Java,Android,Android Context,我的应用程序中有以下代码: String[] filelist = APP.getContext().filelist(); ... public class APP extends Application { static Context theContext; public void onCreate() { super.onCreate(); theContext = this; } public static C

我的应用程序中有以下代码:

String[] filelist = APP.getContext().filelist();  

...

public class APP extends Application {
    static Context theContext;

    public void onCreate() {
        super.onCreate();
        theContext = this;
    }

    public static Context getContext() {
        Log.v("APP", "APP.getContext() called.");
        return theContext;
    }
}
方法
Context.filelist()
返回
NullPointerException
,无论我如何重写此代码。为什么会这样

我可以确认
VERBOSE
登录
getContext()
显示

日志:

D/AndroidRuntime(  342): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime(  342): CheckJNI is ON
D/AndroidRuntime(  342): Calling main entry com.android.commands.am.Am
I/ActivityManager(   83): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.gobernador/.TopPage } from pid 342
I/ActivityManager(   83): Start proc com.gobernador for activity com.gobernador/.TopPage: pid=350 uid=10036 gids={3002}
D/AndroidRuntime(  342): Shutting down VM
D/dalvikvm(  342): GC_CONCURRENT freed 102K, 69% free 319K/1024K, external 0K/0K, paused 1ms+1ms
D/dalvikvm(  342): Debugger has detached; object registry had 1 entries
I/AndroidRuntime(  342): NOTE: attach of thread 'Binder Thread #3' failed
W/dalvikvm(  350): Exception Ljava/lang/NullPointerException; thrown while initializing Lcom/gobernador/APP;
W/dalvikvm(  350): Class init failed in newInstance call (Lcom/gobernador/APP;)
D/AndroidRuntime(  350): Shutting down VM
W/dalvikvm(  350): threadid=1: thread exiting with uncaught exception (group=0x40015560)
I/ARMAssembler(   83): generated scanline__00000177:03515104_00001002_00000000 [ 87 ipp] (110 ins) at [0x444ea6f0:0x444ea8a8] in 1886492 ns
E/AndroidRuntime(  350): FATAL EXCEPTION: main
E/AndroidRuntime(  350): java.lang.ExceptionInInitializerError
E/AndroidRuntime(  350):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(  350):    at java.lang.Class.newInstance(Class.java:1409)
E/AndroidRuntime(  350):    at android.app.Instrumentation.newApplication(Instrumentation.java:957)
E/AndroidRuntime(  350):    at android.app.Instrumentation.newApplication(Instrumentation.java:942)
E/AndroidRuntime(  350):    at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
E/AndroidRuntime(  350):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3260)
E/AndroidRuntime(  350):    at android.app.ActivityThread.access$2200(ActivityThread.java:117)
E/AndroidRuntime(  350):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
E/AndroidRuntime(  350):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  350):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  350):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(  350):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  350):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  350):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(  350):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(  350):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  350): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  350):    at com.gobernador.ReaderWriter.<init>(ReaderWriter.java:20)
E/AndroidRuntime(  350):    at com.gobernador.APP.<clinit>(APP.java:11)
E/AndroidRuntime(  350):    ... 16 more
W/ActivityManager(   83):   Force finishing activity com.gobernador/.TopPage
D/AndroidRuntime(342):>>>>>>>AndroidRuntime START com.android.internal.os.RuntimeInit新字符串[9]是无用的。您可以简单地这样写:

final String[] filelist = APP.getContext().filelist(); 

但这并不能解决你的问题。问题是,要么“APP”为null,要么“APP.getContext()”返回null

上下文在类实例化之前不会初始化,因此在此之前它将为null

使
getContext()
方法非静态,并从该类的实例调用它

一种方法是打电话

((APP)(this.getApplication())).getContext().filelist()

来自应用程序中的其他活动。

什么是应用程序?你确定它不为空吗?你不必初始化数组,一个声明就足够了。应用程序的工作方式非常类似。我应该重新设计我这样做的方式吗?不一定,但你肯定应该显示你的相关代码…接受,因为你让我意识到,在我实际通过
APP.onCreate()
将任何值存储到
context
之前,我正在调用包含有问题的代码行的构造函数。非常感谢。