简体   繁体   English

如何使用另一类的方法? (从Processing导出的android项目)

[英]How do I use methods of another class? (Exported android project from Processing)

My project is an exported android project from Processing. 我的项目是从Processing导出的android项目。 It uses Fontastic library. 它使用Fontastic库。 It always force closes whenever I try to run it on my Android device. 每当我尝试在Android设备上运行它时,它始终会强制关闭。 I was thinking that its because of the font.setup() on the MainActivity but I can not think of any solution. 我在想这是因为MainActivity上的font.setup()但我想不出任何解决方案。 Please take a look on the following lines of code. 请看下面的代码行。 Thank you. 谢谢。

This is the MainActivity.java 这是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();
}
}

and this is the class named Wave.java 这是名为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;
    }
}

You need to create an object of the class whose method you want to use. 您需要创建要使用其方法的类的对象。 If you don't do that you will get NullPointerException because the variable font is not initialized. 如果不这样做,则将得到NullPointerException因为未初始化变量font

Do this 做这个

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

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

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