Libgdx外部存储在Android上导致NullPointerException

时间:2020-12-06 16:53:38

I'm having trouble accessing external storage on Android with the Gdx library. Any help would be appreciated!

我在使用Gdx库访问Android上的外部存储时遇到问题。任何帮助,将不胜感激!

Simplified code:

简化代码:

import android.app.Activity;
import android.os.Bundle;
import com.badlogic.gdx.Gdx;

public class Mp3AndFftTest extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Gdx.files.isExternalStorageAvailable();
    }
}

This causes:

这导致:

ERROR   AndroidRuntime  FATAL EXCEPTION: main
ERROR   AndroidRuntime  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cmbryan.android/com.cmbryan.android.Mp3AndFftTest}: java.lang.NullPointerException
ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
ERROR   AndroidRuntime      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
ERROR   AndroidRuntime      at android.app.ActivityThread.access$1500(ActivityThread.java:117)
ERROR   AndroidRuntime      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:123)
ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:3683)
ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:507)
ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
ERROR   AndroidRuntime      at dalvik.system.NativeStart.main(Native Method)
ERROR   AndroidRuntime  Caused by: java.lang.NullPointerException
ERROR   AndroidRuntime      at com.cmbryan.android.Mp3AndFftTest.onCreate(Mp3AndFftTest.java:17)
ERROR   AndroidRuntime      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
ERROR   AndroidRuntime      ... 11 more

I have correct permissions in the Android manifest, and have tested on the emulator and a real device. What am I missing? Is it because I'm using netbeans, maybe??

我在Android清单中拥有正确的权限,并在模拟器和真实设备上进行了测试。我错过了什么?是因为我使用netbeans,也许?

2 个解决方案

#1


3  

Line 17 is:

第17行是:

Gdx.files.isExternalStorageAvailable();

right? Gdx.files is null. This is because you haven't run the GDX initialization code that sets up the global shortcuts in Gdx. You need to create an ApplicationListener and pass it to AndroidApplication.initialize(..).

对? Gdx.files为null。这是因为您没有运行GDX初始化代码来设置Gdx中的全局快捷方式。您需要创建一个ApplicationListener并将其传递给AndroidApplication.initialize(..)。

Follow the example here: http://code.google.com/p/libgdx/wiki/ProjectSetup

请按照此处的示例操作:http://code.google.com/p/libgdx/wiki/ProjectSetup

libGDX's state is ill-defined until you've completed all the expected initialization. libGDX is more than just a library you can consume pieces out of, its a framework that handles the complete setup and initialization of your app.

在完成所有预期的初始化之前,libGDX的状态是不明确的。 libGDX不仅仅是一个可以使用的库,它是一个处理应用程序完整设置和初始化的框架。

#2


3  

Perhaps not the ideal solution. But you can also use the

也许不是理想的解决方案。但你也可以使用

com.badlogic.gdx.backends.lwjgl.LwjglFiles

class directly. This is the class for creating the Gdx.files object.

直接上课。这是用于创建Gdx.files对象的类。

#1


3  

Line 17 is:

第17行是:

Gdx.files.isExternalStorageAvailable();

right? Gdx.files is null. This is because you haven't run the GDX initialization code that sets up the global shortcuts in Gdx. You need to create an ApplicationListener and pass it to AndroidApplication.initialize(..).

对? Gdx.files为null。这是因为您没有运行GDX初始化代码来设置Gdx中的全局快捷方式。您需要创建一个ApplicationListener并将其传递给AndroidApplication.initialize(..)。

Follow the example here: http://code.google.com/p/libgdx/wiki/ProjectSetup

请按照此处的示例操作:http://code.google.com/p/libgdx/wiki/ProjectSetup

libGDX's state is ill-defined until you've completed all the expected initialization. libGDX is more than just a library you can consume pieces out of, its a framework that handles the complete setup and initialization of your app.

在完成所有预期的初始化之前,libGDX的状态是不明确的。 libGDX不仅仅是一个可以使用的库,它是一个处理应用程序完整设置和初始化的框架。

#2


3  

Perhaps not the ideal solution. But you can also use the

也许不是理想的解决方案。但你也可以使用

com.badlogic.gdx.backends.lwjgl.LwjglFiles

class directly. This is the class for creating the Gdx.files object.

直接上课。这是用于创建Gdx.files对象的类。