【问题标题】:How do I use methods of another class? (Exported android project from Processing)如何使用另一个类的方法? (从 Processing 导出的 android 项目)
【发布时间】:2016-02-15 09:27:16
【问题描述】:

我的项目是从 Processing 导出的 android 项目。它使用 Fontastic 库。每当我尝试在我的 Android 设备上运行它时,它总是强制关闭。我在想这是因为 MainActivity 上的 font.setup() 但我想不出任何解决方案。请查看以下代码行。谢谢。

这是 MainActivity.java

import android.app.Activity;
import android.os.Bundle;   

public class MainActivity extends Activity {
public static Wave font;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    font.setup();
}
}

这是名为 Wave.java

的类
import fontastic.FPoint;
import fontastic.Fontastic;
import processing.core.PApplet;
import processing.core.PFont;

public class Wave extends PApplet {
    Fontastic f;
    float charWidth = 512;
    PFont myFont;
    int version = 0;
    boolean fontBuilt = false;

    public void setup(){
        randomize();
        create();
    }

    public void randomize(){
        version++;
        if (f != null) { f.cleanup(); }
        f = new Fontastic(this, "WaveFont" + nf(version,4));
        f.setAdvanceWidth(PApplet.parseInt(charWidth));
        for (int i=0; i<Fontastic.alphabet.length; i++) {
            char c = Fontastic.alphabet[i];
            FPoint[] points = new FPoint[4];
            float rectSize = charWidth * 0.5f;
            float rnd = charWidth * 0.2f;
            points[0] = new FPoint(charWidth / 2 - rectSize / 2, charWidth / 2 - rectSize / 2);
            points[1] = new FPoint(charWidth / 2 - rectSize / 2, charWidth / 2 + rectSize / 2);
            points[2] = new FPoint(charWidth / 2 + rectSize / 2, charWidth / 2 + rectSize / 2);
            points[3] = new FPoint(charWidth / 2 + rectSize / 2, charWidth / 2 - rectSize / 2);
            points[0].setControlPoint1(points[0].x + rnd, points[0].y + random(-rnd, rnd));
            points[1].setControlPoint1(points[1].x + random(-rnd, rnd), points[1].y - rnd);
            points[2].setControlPoint1(points[2].x - rnd, points[2].y + random(-rnd, rnd));
            points[3].setControlPoint1(points[3].x - random(-rnd, rnd), points[3].y + rnd);
            points[0].setControlPoint2(points[0].x + random(-rnd, rnd), points[0].y + rnd);
            points[1].setControlPoint2(points[1].x + rnd, points[1].y + random(-rnd, rnd));
            points[2].setControlPoint2(points[2].x + random(-rnd, rnd), points[2].y - rnd);
            points[3].setControlPoint2(points[3].x - rnd, points[3].y + random(-rnd, rnd));
            f.addGlyph(c).addContour(points);
        }
    }

    public void create(){
        f.buildFont();
        f.cleanup();
        myFont = createFont(f.getTTFfilename(), 200);
        fontBuilt = true;
    }
}

【问题讨论】:

  • 在从Wave访问方法之前添加font=new Wave();
  • 请不要编辑您的问题以包含新问题。而是创建一个新问题。

标签: java android class methods processing


【解决方案1】:

您需要创建您要使用其方法的类的对象。如果你不这样做,你会得到NullPointerException,因为变量font没有被初始化。

这样做

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    font = new Wave(); // You missed this line
    font.setup();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 2014-03-18
    • 2013-09-24
    • 2012-06-04
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多