我有以下问题。应用程序崩溃并显示错误 sladnje,欢迎提出想法
09-07 20:10:55.896: D/dalvikvm(1347): GC freed 782 objects / 55712 bytes in 109ms
09-07 20:10:56.066: D/dalvikvm(1347): GC freed 162 objects / 7192 bytes in 47ms
09-07 20:10:56.116: W/dalvikvm(1347): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
09-07 20:10:56.126: E/AndroidRuntime(1347): Uncaught handler: thread Thread-9 exiting due to uncaught exception
09-07 20:10:56.126: E/AndroidRuntime(1347): java.lang.NullPointerException
09-07 20:10:56.126: E/AndroidRuntime(1347): at aa.bb.cc.Panel.onDraw(Panel.java:372)
09-07 20:10:56.126: E/AndroidRuntime(1347): at aa.bb.cc.TutorialThread.run(Panel.java:502)
09-07 20:10:56.146: I/dalvikvm(1347): threadid=7: reacting to signal 3
09-07 20:10:56.218: E/dalvikvm(1347): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
09-07 20:10:57.916: I/Process(1347): Sending signal. PID: 1347 SIG: 9
AndroidPaint.java
包aa.bb.cc;
public class AndroidPaint extends Activity {
static final int REQUEST_CODE = 1001;
public Shape currentGraphicObject;
public int ShapeObject_to_be_created;
public int ShapeLine = 1;
public int ShapeRect = 2;
public int ShapeCircle = 3;
public int ShapeOval = 4;
public int ShapeFreehand =5;
public int ShapeErase = 6;
public Paint mPaint;
public float BrushWidth;
public double[] color;
public Panel p;
public int number_of_graphicObjects;
public boolean shapemenuclicked;
public boolean colormenuclicked;
public boolean erasemenuclicked;
public boolean brushwidthmenuclicked;
public ArrayList<Shape> graphicobjects;
private Bitmap wallPaperBitmap;
static WallpaperManager wallpaperManager;
public int wallpaperHeight;
public int wallpaperWidth;
private String mImagePath = Environment.getExternalStorageDirectory() + "/androidpaint";
File file;
public Canvas bitmapCanvas;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
color = new double[3];
requestWindowFeature(Window.FEATURE_NO_TITLE);
graphicobjects = new ArrayList<Shape>();
currentGraphicObject = new Shape();
mPaint = new Paint();
InitializePaint();
shapemenuclicked = false;
colormenuclicked = false;
BrushWidth = 3;
Display display = getWindowManager().getDefaultDisplay();
wallpaperHeight = display.getHeight();
wallpaperWidth = display.getWidth();
wallPaperBitmap = Bitmap.createBitmap(wallpaperWidth, wallpaperHeight, Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(wallPaperBitmap);
}
public void onPause(){
super.onPause();
p.surfaceDestroyed(p.getHolder());
}
public void onStop(){
super.onStop();
p.surfaceDestroyed(p.getHolder());
/*graphicobjects.clear();
number_of_graphicObjects = 0;*/
}
public void onDestroy(){
super.onDestroy();
p.surfaceDestroyed(p.getHolder());
graphicobjects.clear();
number_of_graphicObjects = 0;
}
public void onResume(){
super.onResume();
}
public void onStart(){
super.onStart();
p = new Panel(this,null);
// setContentView(p/*new Panel(this)*/);
setContentView(R.layout.main);
p=(Panel)findViewById(R.id.yourID) ;
/* if (getLastNonConfigurationInstance() != null)
{
//GraphicObject = (Shape)getLastNonConfigurationInstance();
graphicobjects.set(number_of_graphicObjects-1, (Shape)getLastNonConfigurationInstance());
}
int n = graphicobjects.size();
if (getLastNonConfigurationInstance() != null)
{
//GraphicObject = (Shape)getLastNonConfigurationInstance();
//graphicobjects.set(number_of_graphicObjects-1, (Shape)getLastNonConfigurationInstance());
//graphicobjects.addAll((Shape)getLastNonConfigurationInstance());
Object[] temp = getLastNonConfigurationInstance();
for(int i = 0; i<n; i++){
graphicobjects.set(i,(Shape)getLastNonConfigurationInstance());
}
}*/
}
public void onSaveInstanceState(Bundle savedInstanceState){
//savedInstanceState.p
}
public void onRestoreInstanceState(Bundle savedInstanceState){
}
//test
/* public Object onRetainNonConfigurationInstance()
{
// if (GraphicObject != null) // Check that the object exists
//return(GraphicObject);
//if(!graphicobjects.isEmpty())
return graphicobjects.toArray();//.get(number_of_graphicObjects-1);
// return super.onRetainNonConfigurationInstance();
}*/
private void InitializePaint()
{
mPaint.setDither(true);
mPaint.setColor(Color.rgb(100, 100,100));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(BrushWidth);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.itemLine:
ShapeObject_to_be_created = ShapeLine;
//GraphicObject = new Line();
shapemenuclicked = true;
erasemenuclicked = false;
brushwidthmenuclicked = false;
return true;
case R.id.itemFreehand:
ShapeObject_to_be_created = ShapeFreehand;
//GraphicObject = new FreeHand();
shapemenuclicked = true;
erasemenuclicked = false;
brushwidthmenuclicked = false;
return true;
case R.id.itemRectangle:
ShapeObject_to_be_created = ShapeRect;
//GraphicObject = new Rectangle();
shapemenuclicked = true;
erasemenuclicked = false;
brushwidthmenuclicked = false;
return true;
case R.id.itemCircle:
ShapeObject_to_be_created = ShapeCircle;
//GraphicObject = new Circle();
shapemenuclicked = true;
erasemenuclicked = false;
brushwidthmenuclicked = false;
return true;
case R.id.itemOval:
ShapeObject_to_be_created = ShapeOval;
//GraphicObject = new Oval();
shapemenuclicked = true;
erasemenuclicked = false;
brushwidthmenuclicked = false;
return true;
case R.id.itemColor:
LaunchColorPicker();
colormenuclicked = true;
erasemenuclicked = false;
brushwidthmenuclicked = false;
return true;
case R.id.itemErase:
ShapeObject_to_be_created = ShapeErase;
//GraphicObject = new Erase();
erasemenuclicked = true;
shapemenuclicked = false;
colormenuclicked = false;
brushwidthmenuclicked = false;
//BrushWidth = 8;
return true;
case R.id.itemBrushWidth3:
BrushWidth = 3;
brushwidthmenuclicked = true;
return true;
case R.id.itemBrushWidth4:
BrushWidth = 6;
brushwidthmenuclicked = true;
return true;
case R.id.itemBrushWidth5:
BrushWidth =9;
brushwidthmenuclicked = true;
return true;
case R.id.itemBrushWidth6:
BrushWidth = 12;
brushwidthmenuclicked = true;
return true;
case R.id.itemSetWallPaper:
//wallPaperBitmap = Bitmap.createBitmap(p.getWidth(),p.getHeight(),Bitmap.Config.ARGB_8888);
//Bitmap temp = p.getDrawingCache(true);
//wallPaperBitmap = Bitmap.createBitmap(temp);
//Canv
//Canvas canvas = p.getHolder().lockCanvas();
//p.getHolder().lockCanvas().drawBitmap(wallPaperBitmap, 0,0, mPaint);
//p.draw(canvas);
//try{
//getApplicationContext().setWallpaper(wallPaperBitmap);
//}
//catch(IOException e){
//e.printStackTrace();
//}
//p.saveScreenshot();
//wallPaperBitmap = Bitmap.createBitmap(this.p.getWidth(), this.p.getHeight(), Bitmap.Config.ARGB_8888);
//File file = new File(mScreenshotPath + "/" + "screeshot.jpg");
/*try{
FileInputStream fIS = new FileInputStream(file);
//wallPaperBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fIS);
fIS.close();
} catch (FileNotFoundException e) {
Log.e("Panel", "FileNotFoundException", e);
} catch (IOException e) {
Log.e("Panel", "IOEception", e);
}*/
return true;
case R.id.itemSaveImage:
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMMddHmmss");
String dateNow = formatter.format(currentDate.getTime());
file = new File(mImagePath + "/" + dateNow +".9.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
wallPaperBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.e("Panel", "FileNotFoundException", e);
}
catch (IOException e) {
Log.e("Panel", "IOEception", e);
}
}
return false;
}
private void LaunchColorPicker(){
Intent launchcolorpicker = new Intent();
launchcolorpicker.setClassName("somitsolutions.training.android.colorpicker", "somitsolutions.training.android.colorpicker.ColorPicker");
launchcolorpicker.setAction("somitsolutions.training.android.colorpicker.android.intent.action.COLORPICKER");
launchcolorpicker.addCategory("CATEGORY_DEFAULT");
launchcolorpicker.setType("vnd.somitsolutions.color/vnd.somitsolutions.color-value");
try {
startActivityForResult(launchcolorpicker,REQUEST_CODE);
}
catch(ActivityNotFoundException e){
Log.e("IntentExample", "Activity could not be started...");
}
}
public void onActivityResult(int requestcode, int resultcode, Intent result ) {
if(requestcode == REQUEST_CODE){
if(resultcode == RESULT_OK){
color[0] = (result.getDoubleArrayExtra("somitsolutions.training.android.colorpicker.color_of_the_shape"))[0];
color[1] = (result.getDoubleArrayExtra("somitsolutions.training.android.colorpicker.color_of_the_shape"))[1];
color[2] = (result.getDoubleArrayExtra("somitsolutions.training.android.colorpicker.color_of_the_shape"))[2];
//mPaint.setColor(Color.rgb((int)color[0], (int)color[1],(int)color[2]));
//GraphicObject.setPaintColor((int)color[0], (int)color[1],(int)color[2]);
//test
//onStart();
}
}
}}
调色板.java
package aa.bb.cc;
public class Panel extends SurfaceView implements Callback {
private Shape currentGraphicObject;
AndroidPaint a;
public Panel(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
setFocusable(true);
Bitmap.createBitmap(this.getWidth(),this.getHeight(),Bitmap.Config.ARGB_8888);
setDrawingCacheEnabled(true);
}
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
setFocusable(true);
Bitmap.createBitmap(this.getWidth(),this.getHeight(),Bitmap.Config.ARGB_8888);
setDrawingCacheEnabled(true);
}
public ArrayList<Shape> getGraphicObjects(){
return a.graphicobjects;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//if(shapemenuclicked == true && erasemenuclicked == false){
synchronized (_thread.getSurfaceHolder()) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(a.shapemenuclicked == true && a.erasemenuclicked == false){
//if(GraphicObject instanceof Line){
if(a.ShapeObject_to_be_created == a.ShapeLine){
currentGraphicObject = new Line();
((Line) currentGraphicObject).getBegin().setX(event.getX());
//((Line)(GraphicObject).getBegin().setX(event.getX()));//.setX(event.getX());
//begin.setY(event.getY());
((Line) currentGraphicObject).getBegin().setY(event.getY());
}
//if(GraphicObject instanceof FreeHand){
if(a.ShapeObject_to_be_created == a.ShapeFreehand){
currentGraphicObject = new FreeHand();
((FreeHand)currentGraphicObject).getPath().moveTo(event.getX(), event.getY());
((FreeHand)currentGraphicObject).getPath().lineTo(event.getX(), event.getY());
}
if(a.ShapeObject_to_be_created == a.ShapeRect){
currentGraphicObject = new Rectangle();
Point temp = new Point(event.getX(), event.getY());
((Rectangle) currentGraphicObject).settemppointOfOneEndRectangle(temp);
}
if(a.ShapeObject_to_be_created == a.ShapeCircle){
currentGraphicObject = new Circle();
((Circle)currentGraphicObject).getOneEndOfTheCircle().setX(event.getX());
((Circle)currentGraphicObject).getOneEndOfTheCircle().setY(event.getY());
}
if(a.ShapeObject_to_be_created == a.ShapeOval){
currentGraphicObject = new Oval();
((Oval)currentGraphicObject).getoneEndOfTheOval().setX(event.getX());
((Oval)currentGraphicObject).getoneEndOfTheOval().setY(event.getY());
}
}
if(a.shapemenuclicked == false && a.erasemenuclicked == true){
if(a.ShapeObject_to_be_created == a.ShapeErase){
currentGraphicObject = new Erase();
((Erase)currentGraphicObject).getPath().moveTo(event.getX(), event.getY());
((Erase)currentGraphicObject).getPath().lineTo(event.getX(), event.getY());
}
}
}
else if(event.getAction() == MotionEvent.ACTION_MOVE){
if(a.shapemenuclicked == true && a.erasemenuclicked == false){
if(a.ShapeObject_to_be_created == a.ShapeFreehand){
((FreeHand)currentGraphicObject).getPath().lineTo(event.getX(), event.getY());
}
if(a.ShapeObject_to_be_created == a.ShapeLine){
}
if(a.ShapeObject_to_be_created == a.ShapeRect){
}
if(a.ShapeObject_to_be_created == a.ShapeCircle){
}
if(a.ShapeObject_to_be_created == a.ShapeOval){
}
}
if(a.shapemenuclicked == false && a.erasemenuclicked == true){
if(a.ShapeObject_to_be_created == a.ShapeErase){
((Erase)currentGraphicObject).getPath().lineTo(event.getX(), event.getY());
}
}
else if(event.getAction() == MotionEvent.ACTION_UP){
if(a.shapemenuclicked == true && a.erasemenuclicked == false){
//if(GraphicObject instanceof Line){
if(a.ShapeObject_to_be_created == a.ShapeLine){
((Line) currentGraphicObject).getEnd().setX(event.getX());
((Line) currentGraphicObject).getEnd().setY(event.getY());
Point temp_begin = ((Line)currentGraphicObject).getBegin();
Point temp_end = ((Line)currentGraphicObject).getEnd();
((Line) currentGraphicObject).setBegin(temp_begin);
((Line)currentGraphicObject).getPath().moveTo(temp_begin.getX(), temp_begin.getY());
((Line)currentGraphicObject).getPath().lineTo(temp_end.getX(), temp_end.getY());
//currentGraphicObject.setrgb((int)color[0],(int)color[1],(int)color[2]);
}
if(a.ShapeObject_to_be_created == a.ShapeFreehand){
((FreeHand)currentGraphicObject).getPath().lineTo(event.getX(), event.getY());
((FreeHand)currentGraphicObject).getGraphicsPath().add(((FreeHand)currentGraphicObject).getPath());
}
if(a.ShapeObject_to_be_created == a.ShapeRect){
float tempX = ((Rectangle) currentGraphicObject).gettemppointOfOneEndRectangle().getX();
float tempY = ((Rectangle) currentGraphicObject).gettemppointOfOneEndRectangle().getY();
float tempX1 = event.getX();
float tempY1 = event.getY();
if(tempX<tempX1 && tempY>tempY1){
((Rectangle)currentGraphicObject).getPath().addRect(tempX, tempY1, tempX1, tempY, Path.Direction.CW);
}
if(tempX<tempX1 && tempY<tempY1){
((Rectangle)currentGraphicObject).getPath().addRect(tempX, tempY, tempX1, tempY1, Path.Direction.CW);
}
if(tempX>tempX1 && tempY>tempY1){
((Rectangle)currentGraphicObject).getPath().addRect(tempX1, tempY1, tempX, tempY, Path.Direction.CW);
}
if(tempX>tempX1 && tempY<tempY1){
((Rectangle)currentGraphicObject).getPath().addRect(tempX1, tempY, tempX, tempY1, Path.Direction.CW);
}
}
//if(GraphicObject instanceof Circle){
if(a.ShapeObject_to_be_created == a.ShapeCircle){
float tempX1 = ((Circle)currentGraphicObject).getOneEndOfTheCircle().getX();
float tempY1 = ((Circle)currentGraphicObject).getOneEndOfTheCircle().getY();
float tempX2 = event.getX();
float tempY2 = event.getY();
double temp = Math.pow((tempX1-tempX2),2) + Math.pow((tempY1-tempY2),2);
float radius = (float)Math.sqrt(temp)/2;
((Circle)currentGraphicObject).getPath().addCircle((tempX1 + tempX2)/2,(tempY1 + tempY2)/2, radius, Path.Direction.CW);
(int)color[1],(int)color[2]);
}
if(a.ShapeObject_to_be_created == a.ShapeOval){
float tempX = ((Oval)currentGraphicObject).getoneEndOfTheOval().getX();
float tempY = ((Oval)currentGraphicObject).getoneEndOfTheOval().getY();
float tempX1 = event.getX();
float tempY1 = event.getY();
if(tempX<=tempX1 && tempY>=tempY1){
((Oval)currentGraphicObject).getRectangle().set(tempX,tempY1,tempX1,tempY);
}
if(tempX<=tempX1 && tempY<=tempY1){
((Oval)currentGraphicObject).getRectangle().set(tempX,tempY,tempX1,tempY1);
}
if(tempX>=tempX1 && tempY>=tempY1){
((Oval)currentGraphicObject).getRectangle().set(tempX1,tempY1,tempX,tempY);
}
if(tempX>=tempX1 && tempY<=tempY1){
((Oval)currentGraphicObject).getRectangle().set(tempX1,tempY,tempX,tempY1);
}
//RectF r = ((Oval)GraphicObject).getRectangle();
((Oval)currentGraphicObject).getPath().addOval(((Oval)currentGraphicObject).getRectangle(), Path.Direction.CW);
//((Oval)GraphicObject).getPath().addOval(r, Path.Direction.CW);
}
}
if(a.shapemenuclicked == false && a.erasemenuclicked == true){
//if(GraphicObject instanceof Erase){
if(a.ShapeObject_to_be_created == a.ShapeErase){
((Erase)currentGraphicObject).getPath().lineTo(event.getX(), event.getY());
((Erase)currentGraphicObject).getGraphicsPath().add(((Erase)currentGraphicObject).getPath());
}
}
if(a.colormenuclicked == false && a.shapemenuclicked == true){
currentGraphicObject.setrgb(100,100,100);
}
if(a.colormenuclicked == true && a.shapemenuclicked == true){
currentGraphicObject.setrgb((int)a.color[0],(int)a.color[1],(int)a.color[2]);
}
if(a.erasemenuclicked == true && a.colormenuclicked == false){
currentGraphicObject.setrgb(0,0,0);
//mPaint.setStrokeWidth(6);
}
if(a.brushwidthmenuclicked == true){// || erasemenuclicked == true){
currentGraphicObject.setStrokeWidth(a.BrushWidth);
}
a.graphicobjects.add(currentGraphicObject);
a.number_of_graphicObjects++;
}
/*if(colormenuclicked == false && shapemenuclicked == true){
currentGraphicObject.setrgb(100,100,100);
}
if(colormenuclicked == true && shapemenuclicked == true){
currentGraphicObject.setrgb((int)color[0],(int)color[1],(int)color[2]);
}
if(erasemenuclicked == true && colormenuclicked == false){
currentGraphicObject.setrgb(0,0,0);
//mPaint.setStrokeWidth(6);
}
if(brushwidthmenuclicked == true){// || erasemenuclicked == true){
currentGraphicObject.setStrokeWidth(BrushWidth);
}
graphicobjects.add(currentGraphicObject);
number_of_graphicObjects++;*/
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
for(int i = 0; i<a.number_of_graphicObjects; i++){
Shape currentGraphicObject = a.graphicobjects.get(i);
a.mPaint.setColor(Color.rgb(currentGraphicObject.getrgb()[0], currentGraphicObject.getrgb()[1], currentGraphicObject.getrgb()[2]));
a.mPaint.setStrokeWidth(currentGraphicObject.getStrokeWidth());
if(currentGraphicObject instanceof FreeHand){
for (Path path : ((FreeHand)currentGraphicObject).getGraphicsPath()) {
canvas.drawPath(path,a.mPaint);
a.bitmapCanvas.drawPath(path,a.mPaint);
}
}
else if(currentGraphicObject instanceof Erase){
for (Path path : ((Erase)currentGraphicObject).getGraphicsPath()) {
canvas.drawPath(path, a.mPaint);
a.bitmapCanvas.drawPath(path, a.mPaint);
}
}
else{
canvas.drawPath(currentGraphicObject.getPath(),a.mPaint);
a.bitmapCanvas.drawPath(currentGraphicObject.getPath(),a.mPaint);
}
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
}
} finally {
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}