如何将ana arraylist放入另一个类的数组中?

时间:2013-06-08 03:29:02

标签: java android opengl-es arraylist

我试图将数组列表(linep)和(indexP)放入数组(顶点)和(pIndex)中,我需要在opengl中渲染一行....最有可能是对drawframe()的调用?我不知道该怎么做我继续得到像数组列表的nullpointerexception等错误...?

public class MainActivity extends Activity implements OnClickListener {
public ArrayList<Short> indexP = new ArrayList<Short>();
public ArrayList<Float> linep = new ArrayList<Float>();
public Float coords = (float) 0;
public short p = 0;
public int l = 0;
GLSurfaceView ourSurface;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cad);
    ourSurface = new GLSurfaceView(this);
    FrameLayout v = (FrameLayout) findViewById(R.id.display);
    v.addView(ourSurface);
    ourSurface.setRenderer(new GLRenderer());


    Button line = (Button) findViewById(R.id.line);
    final Button enter = (Button) findViewById(R.id.enter);
    EditText cl = (EditText) findViewById(R.id.cl);
    final String value = cl.getText().toString();

    try {
        coords = Float.parseFloat(value);
    } catch (NumberFormatException e) {};

    line.setOnClickListener(this);
    enter.setOnClickListener(this);

}

public void onClick(View v) {
    Object pIndex =null;
    Object vertices = null;
    TextView info = (TextView) findViewById(R.id.info);
    switch (v.getId()) {
    case R.id.line:
        info.setText("Input X");
    case R.id.enter:
        switch (l) {
        case (0):
            linep.add(coords);
            l++;
            info.setText("Input Y");
            break;
        case (1):
            linep.add(coords);
            indexP.add(p);
            l = 0;
            p++;
            linep.toArray(vertices);
            indexP.toArray(pIndex);
            break;
        }
    }

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

}

public class Object {

 public float vertices []={};//  this is the array for coords

    private float rgbaVals []={
            1,1,0,.5f,
            .25f,0,.85f,1,
            0,1,1,1
        };

    private FloatBuffer colorBuff;

    private FloatBuffer vertBuff;

    public short[] pIndex ={0,1,2};// this is the array for the drawing order

    private ShortBuffer pBuff;

    public Object(){
        ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
        bBuff.order(ByteOrder.nativeOrder());
        vertBuff = bBuff.asFloatBuffer();
        vertBuff.put(vertices);
        vertBuff.position(0);

        ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
        pbBuff.order(ByteOrder.nativeOrder());
        pBuff = pbBuff.asShortBuffer();
        pBuff.put(pIndex);
        pBuff.position(0);

        ByteBuffer cBuff = ByteBuffer.allocateDirect(rgbaVals.length * 4);
        cBuff.order(ByteOrder.nativeOrder());
        colorBuff = cBuff.asFloatBuffer();
        colorBuff.put(rgbaVals);
        colorBuff.position(0);
    }

    public void draw(GL10 gl) {
        // TODO Auto-generated method stub
        gl.glFrontFace(GL10.GL_CW);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
        gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

}

public class GLRenderer implements Renderer {
private Object line;

public GLRenderer(){
    line = new Object();

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glClearColor(.8f, 0f, .2f, 1f);
    gl.glClearDepthf(1f);
}



@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, -5, 0, 0, 0, 0, 2, 0);
    line.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    gl.glViewport(0, 0, width, height);
    float ratio = (float) width/height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
}

}

06-07 23:07:01.552: W/dalvikvm(2056): threadid=1: thread exiting with uncaught exception (group=0x40a961f8)
06-07 23:07:01.568: E/AndroidRuntime(2056): FATAL EXCEPTION: main
06-07 23:07:01.568: E/AndroidRuntime(2056): java.lang.NullPointerException
06-07 23:07:01.568: E/AndroidRuntime(2056):     at java.util.ArrayList.toArray(ArrayList.java:514)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at com.example.linecad.MainActivity.onClick(MainActivity.java:67)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at android.view.View.performClick(View.java:3540)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at android.view.View$PerformClick.run(View.java:14167)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at android.os.Handler.handleCallback(Handler.java:605)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at android.os.Looper.loop(Looper.java:137)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at android.app.ActivityThread.main(ActivityThread.java:4586)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at java.lang.reflect.Method.invoke(Method.java:511)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-07 23:07:01.568: E/AndroidRuntime(2056):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您看到的NPE是正常的,并且是由于误导了List的{​​{3}}:您将null引用作为参数传递给它。

您需要将vertices声明为float[],而不是Object(为什么这样做?),pIndex作为short[]并执行:

vertices = linep.toArray(new float[linep.size()]);
pIndex = indexP.toArray(new short[indexP.size()]);

这种方法并不简单,你应该好好看看Javadoc(上面的链接)。简而言之,作为参数传递的数组有两个目的:

  • 它用作生成数组的可能的容器;
  • 由于泛型在运行时被丢弃,它也可以作为类型提示,以便将结果转换为正确的类型。