2 u011356791 u011356791 于 2013.11.20 12:06 提问

Android 求帮忙看看这段程序错在哪 在Android虚拟机上运行停止

package com.example.change1;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button btnSimpleDraw, btnTimerDraw;
SurfaceView sfv;
SurfaceHolder sfh;

private Timer mTimer;
private MyTimerTask mTimerTask;
int Y_axis[],//保存Y轴上的点
centerY,//中心线
oldX,oldY,//上一个XY点 
currentX;//当前绘制到的X轴上的点
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnSimpleDraw = (Button) this.findViewById(R.id.drawbotton);
    btnTimerDraw = (Button) this.findViewById(R.id.timerbotton);
    btnSimpleDraw.setOnClickListener(new ClickEvent());
    btnTimerDraw.setOnClickListener(new ClickEvent());
    sfv = (SurfaceView) this.findViewById(R.id.surfaceview);
    sfh = sfv.getHolder();

    //动态绘制
    mTimer = new Timer();
    mTimerTask = new MyTimerTask();     
    String string = "";
    String mString = null;
    //File file = new File("/mnt/sdcard/abc.txt");//读取txt中数据
    try {
        FileInputStream fis=new FileInputStream(Environment.getExternalStorageDirectory()+"/abc.txt");
        byte buffer[]=new byte[1024];
        ByteArrayOutputStream baops=new ByteArrayOutputStream();
        int length=0;
        try {
            while((length=fis.read(buffer))!=-1){
                baops.write(buffer, 0, length);
                string=baops.toString();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    //RandomAccessFile randomAccessFile = null;

// try {
//

// //randomAccessFile = new RandomAccessFile(file,"rw");
// } catch (FileNotFoundException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }

// try {
// while((mString=randomAccessFile.readLine())!= null) ;
//

// System.out.println(mString+"只拿去了");
// string += mString;
// } catch (Exception e) {
// // TODO: handle exception
// }
String[] strings = string.split(",");

    int[] array = new int[strings.length];

    for(int i=0;i<array.length;i++){
        if(strings[i]=="" || strings[i].equals("")){

        }else{
        array[i] = Integer.valueOf(strings[i]);
        }
    }
    // 初始化y轴数据
    centerY = (getWindowManager().getDefaultDisplay().getHeight() - sfv
            .getTop()) / 2;
    Y_axis = new int[getWindowManager().getDefaultDisplay().getWidth()];
    for (int i = 1; i < Y_axis.length; i++) {// array中的数据复制给Y_axis
        Y_axis[i-1] = array[i];
    }
}

class ClickEvent implements View.OnClickListener {  //点击事件

    @Override
    public void onClick(View v) {

        if (v == btnSimpleDraw) {
            SimpleDraw(Y_axis.length-1);//直接绘制

        } else if (v == btnTimerDraw) {
            oldY = centerY;
            mTimer.schedule(mTimerTask, 0, 5);//动态绘制
        }

    }

}

class MyTimerTask extends TimerTask {
    @Override
    public void run() {

        SimpleDraw(currentX);
        currentX++;//往前进
        if (currentX == Y_axis.length - 1) {//如果到了终点,则清屏重来
            ClearDraw();
            currentX = 0;
            oldY = centerY;
        }
    }

}

/*
 * 绘制指定区域
 */
void SimpleDraw(int length) {
    if (length == 0)
        oldX = 0;
    Canvas canvas = sfh.lockCanvas(new Rect(oldX, 0, oldX + length,
            getWindowManager().getDefaultDisplay().getHeight()));// 关键:获取画布
    Log.i("Canvas:",
            String.valueOf(oldX) + "," + String.valueOf(oldX + length));

    Paint mPaint = new Paint();
    mPaint.setColor(Color.GREEN);// 画笔为绿色
    mPaint.setStrokeWidth(2);// 设置画笔粗细

    int y;
    for (int i = oldX + 1; i < length; i++) {// 绘画正弦波
        y = Y_axis[i - 1];
        canvas.drawLine(oldX, oldY, i, y, mPaint);
        oldX = i;
        oldY = y;
    }
    sfh.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
}

void ClearDraw() {
    Canvas canvas = sfh.lockCanvas(null);
    canvas.drawColor(Color.BLACK);// 清除画布
    sfh.unlockCanvasAndPost(canvas);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1个回答

x13945
x13945   2013.11.26 19:42

log在哪里?x13945

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!