简体   繁体   English

将上下文传递到非活动类以使用AssetManager.getAssets()

[英]Pass context to a non-Activity Class to use AssetManager.getAssets()

I am working on an android app and I am trying to open an xml file in assets folder containing references to images located also in assets folder. 我正在开发一个Android应用,并且正在尝试在Assets文件夹中打开一个xml文件,其中包含对也位于Assets文件夹中的图像的引用。 I am using AssetManager in a non-activity Class and tried to pass context to its constructor with no success.There are no errors in Eclipse but when I run the app it crushes. 我在非活动类中使用AssetManager并尝试将上下文传递给其构造函数,但没有成功。Eclipse中没有错误,但是当我运行该应用程序时,它会崩溃。 The code I use for the 2 classes and the XML is below. 我用于2个类和XML的代码如下。

XML File: XML档案:

<scene>

   <sprite>
     <img src="100789.jpg" positionx="100" positiony="200" name="100789.jpg"/>
   </sprite>

   <sprite>
     <img src="100788.jpg" positionx="2" positiony="20" name="100788" />
   </sprite>

</scene> 

Main Class (Activity) 主班(活动)

import android.content.Context;
....

public class Test extends AndroidGame {

     Context context=getApplicationContext();

     @Override
     public Screen getStartScreen() {
         return new LoadingScreen(this, context); 
   }
}

Load Class (Screen) 负荷等级(屏幕)

import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import android.content.Context;
import android.content.res.AssetManager;
....

public class LoadingScreen extends Screen {
    private Context context;
    public Document doc;

  public LoadingScreen(Game game, Context context) {
    super(game);
    this.context=context;
}

AssetManager gi = con.getAssets();

@Override
public void update(float deltaTime) {


try {

    InputStream inStream = gi.open("assets.xml");
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(inStream);
    doc.getDocumentElement().normalize();


}catch (Exception e) {
  e.printStackTrace();
  }


    Graphics g = game.getGraphics();
    Assets.background = g.newPixmap(doc.getDocumentElement().getElementsByTagName("sprite").item(0).getChildNodes().item(1).getAttributes().getNamedItem("name").getNodeValue(), PixmapFormat.RGB565);
    ....
    ....
    game.setScreen(new MainMenuScreen(game));




}

So am I doing something wrong ? 那我做错什么了吗?

I have also tried to put the following code in the constructor with no success: 我还尝试将以下代码放入构造函数中,但没有成功:

public LoadingScreen(Game game, Context context) {
    super(game);
    this.context=context;

AssetManager gi = con.getAssets();
try {
    InputStream inStream = gi.open("assets.xml");
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(inStream);
    doc.getDocumentElement().normalize();


}catch (Exception e) {
  e.printStackTrace();
  }
}

You are trying to access context (the Context member) before you initialise it, causing a NullPointerException . 您尝试在初始化Context之前访问contextContext成员),从而导致NullPointerException move AssetManager gi = con.getAssets(); 移动AssetManager gi = con.getAssets(); inside the method 方法内部

@Override
public void update(float deltaTime) {
  AssetManager gi = context.getAssets();
   ...

}

Since AndroidGame class inherits from an Activity you have to keep in mind the activity life cycle. 由于AndroidGame类是从Activity继承的,因此您必须牢记Activity的生命周期。 An activity doesn't have a Context until just before onCreate() is called and calling getApplicationContext() before then will return null. 直到调用onCreate()之前,活动没有Context ,在此之前调用getApplicationContext()将返回null。 So you need to set the context field inside of onCreate. 因此,您需要在onCreate内设置上下文字段。

Basically 基本上

public class Test extends AndroidGame {

    private Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.context = getApplicationContext();
    }

    ...

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM