0
我有一個問題,在屏幕上繪製自定義視圖時出現空指針異常,但不知道實際問題出現的位置,並且無法找到生成時缺少的東西例外。繪製視圖時出現NullPointerException
所以請建議我爲這個問題的正確解決方案。
錯誤堆棧:
02-24 14:18:46.181: E/AndroidRuntime(7547): FATAL EXCEPTION: main
02-24 14:18:46.181: E/AndroidRuntime(7547): java.lang.NullPointerException
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.graphics.Canvas.drawBitmap(Canvas.java:980)
02-24 14:18:46.181: E/AndroidRuntime(7547): at com.TestFace.FaceDetect.DrawView.onDraw(DrawView.java:89)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.View.draw(View.java:6740)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.View.draw(View.java:6743)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.widget.FrameLayout.draw(FrameLayout.java:352)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.View.draw(View.java:6743)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.widget.FrameLayout.draw(FrameLayout.java:352)
02-24 14:18:46.181: E/AndroidRuntime(7547): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1847)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewRoot.draw(ViewRoot.java:1407)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.os.Looper.loop(Looper.java:123)
02-24 14:18:46.181: E/AndroidRuntime(7547): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-24 14:18:46.181: E/AndroidRuntime(7547): at java.lang.reflect.Method.invokeNative(Native Method)
02-24 14:18:46.181: E/AndroidRuntime(7547): at java.lang.reflect.Method.invoke(Method.java:521)
02-24 14:18:46.181: E/AndroidRuntime(7547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-24 14:18:46.181: E/AndroidRuntime(7547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-24 14:18:46.181: E/AndroidRuntime(7547): at dalvik.system.NativeStart.main(Native Method)
代碼:
package com.TestFace.FaceDetect;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.FaceDetector;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.TestFace.FaceDetect.Util.ColorBall;
import com.TestFace.FaceDetect.Util.FinalImageUploader;
import com.TestFace.FaceDetect.Util.FinalJSONParser;
import com.TestFace.FaceDetect.Util.ResultPack;
public class ProcessImage extends Activity{
LinearLayout image;
Handler errorHandler;
Typeface face;
private File pic;
Handler responseHandler;
Bitmap scaledImage;
Handler startThreadHandler;
Thread statusCheckerTH;
TextView title;
FinalImageUploader uploader;
Thread uploaderTH;
int responseCode;
//ResultPack result = ResultPack.getSingletonObject();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.process_image);
image = (LinearLayout)findViewById(R.id.linearLayout1);
Bitmap img = BitmapFactory.decodeFile(getIntent().getExtras().getString("PATH"));
//image.setImageBitmap(img);
this.uploader = new FinalImageUploader();
if (img == null)
finish();
scaledImage = scaleImage(img);
if(scaledImage!=null)
{
pic = new File(getIntent().getExtras().getString("PATH"));
try {
FileOutputStream fos = new FileOutputStream(pic.getPath());
scaledImage.compress(Bitmap.CompressFormat.JPEG, 95, fos);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
final ProgressDialog dialog = ProgressDialog.show(ProcessImage.this, "FaceDetection ", "Loading.... ", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
// System.out.println("The id after Save:"+id.get(0).toString());
dialog.dismiss();
if(responseCode==0)
{
// setContentView(new MyView(ProcessImage.this));
//Toast.makeText(ProcessImage.this, "Face is not dedected", Toast.LENGTH_SHORT).show();
showDialog("Error", "Face not Dedected");
}
else
{
// dialog.dismiss();
finish();
//setContentView(new DrawView(ProcessImage.this));
Intent intent = new Intent(ProcessImage.this,REsultedImage.class);
startActivity(intent);
Toast.makeText(ProcessImage.this, "Hurray Face is dedected!!!!!!!", Toast.LENGTH_SHORT).show();
}
}
};
final Thread checkUpdate = new Thread() {
public void run() {
try
{
//setCameraDisplayOrientation(ProcessImage.this, cameraId, camera);
String responseString = uploader.uploadPicandSendRequest(new File(pic.getPath()));
System.out.println("the REsponse String=====================================>"+responseString);
FinalJSONParser parser = new FinalJSONParser(responseString);
responseCode = parser.getData();
if(responseCode==0)
{
/* dialog.dismiss();
Toast.makeText(ProcessImage.this, "Face is not dedected", Toast.LENGTH_SHORT).show();*/
handler.sendEmptyMessage(0);
}
else
{
//Toast.makeText(ProcessImage.this, "Hurray Face is dedected!!!!!!!", Toast.LENGTH_SHORT).show();
//image.addView(new MyView(ProcessImage.this));
handler.sendEmptyMessage(0);
}
/* Message localMessage1 = new Message();
localMessage1.obj = localObject;
ProcessImage.this.responseHandler.sendMessage(localMessage1);*/
//return;
}
catch (ClientProtocolException localClientProtocolException)
{
while (true)
{
/* Object localObject = new Message();
((Message)localObject).obj = "Connection Error!";
ProcessImage.this.errorHandler.sendMessage((Message)localObject);*/
localClientProtocolException.printStackTrace();
}
}
catch (IOException localIOException)
{
while (true)
{
/*Message localMessage2 = new Message();
localMessage2.obj = "Connection Error!";
ProcessImage.this.errorHandler.sendMessage(localMessage2);*/
localIOException.printStackTrace();
}
}
catch (Exception localException)
{
localException.printStackTrace();
}
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
/*ProcessImage.this.uploaderTH.start();
this.responseHandler = new Handler()
{
public void handleMessage(Message paramMessage)
{
System.out.println("Message==========================================>"+paramMessage);
}
};
this.startThreadHandler = new Handler()
{
public void handleMessage(Message paramMessage)
{
ProcessImage.this.uploaderTH.start();
}
};
this.errorHandler = new Handler()
{
public void handleMessage(Message paramMessage)
{
String str = (String)paramMessage.obj;
ProcessImage.this.showDialog("Error!", str);
}
};
this.uploaderTH = new Thread(new Runnable()
{
public void run()
{
try
{
String responseString = uploader.uploadPicandSendRequest(ProcessImage.this.pic);
FinalJSONParser parser = new FinalJSONParser(responseString);
parser.getData();
Message localMessage1 = new Message();
localMessage1.obj = localObject;
ProcessImage.this.responseHandler.sendMessage(localMessage1);
//return;
}
catch (ClientProtocolException localClientProtocolException)
{
while (true)
{
Object localObject = new Message();
((Message)localObject).obj = "Connection Error!";
ProcessImage.this.errorHandler.sendMessage((Message)localObject);
localClientProtocolException.printStackTrace();
}
}
catch (IOException localIOException)
{
while (true)
{
Message localMessage2 = new Message();
localMessage2.obj = "Connection Error!";
ProcessImage.this.errorHandler.sendMessage(localMessage2);
localIOException.printStackTrace();
}
}
catch (Exception localException)
{
while (true)
{
Message localMessage2 = new Message();
localMessage2.obj = "Faces not found in your photo!";
ProcessImage.this.errorHandler.sendMessage(localMessage2);
localException.printStackTrace();
}
}
}
}); */
}
private Bitmap scaleImage(Bitmap paramBitmap) {
// TODO Auto-generated method stub
int i = paramBitmap.getWidth();
int j = paramBitmap.getHeight();
System.out.println("The width:"+i);
System.out.println("The Height:"+j);
/*float f2 = j/i * 600;
float f1 = 600/i;
f2 /= j;*/
float scaleWidth = ((float)200)/i;
float scaleHeight = ((float)200)/j;
System.out.println("The value of f1 and f2:"+scaleWidth+" and "+scaleHeight);
Matrix localMatrix = new Matrix();
localMatrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(paramBitmap, 0, 0, i, j, localMatrix, true);
}
private void showDialog(String paramString1, String paramString2)
{
AlertDialog localAlertDialog = new AlertDialog.Builder(this).create();
localAlertDialog.setTitle(paramString1);
localAlertDialog.setMessage(paramString2);
localAlertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramDialogInterface, int paramInt)
{
paramDialogInterface.dismiss();
ProcessImage.this.finish();
}
});
localAlertDialog.show();
}
}
drawView函數:
package com.TestFace.FaceDetect;
import java.io.IOException;
import java.net.MalformedURLException;
import com.TestFace.FaceDetect.Util.ColorBall;
import com.TestFace.FaceDetect.Util.ResultPack;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.FaceDetector;
import android.view.MotionEvent;
import android.view.View;
public class DrawView extends View {
private Bitmap myBitmap;
private Bitmap myBitmap_Circle;
private int width, height;
private FaceDetector.Face[] detectedFaces;
private int NUMBER_OF_FACES=1;
private FaceDetector faceDetector;
private int NUMBER_OF_FACE_DETECTED;
private float eyeDistance;
ResultPack result = ResultPack.getSingletonObject();
private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
private int balID = 0; // variable to know what ball is being dragged
//ResultPack result = new ResultPack();
public DrawView(Context context)
{
super(context);
setFocusable(true); //necessary for getting the touch events
// setting the start point for the balls
Point point1 = new Point();
point1.x = (int)result.getEyeLeftX()+50;
point1.y = (int)result.getEyeLeftY()+25;
Point point2 = new Point();
point2.x = (int)result.getEyeRightX()+50;
point2.y = (int)result.getEyeRightY()+25;
Point point3 = new Point();
point3.x = (int)result.getMouthCenterX()+50;
point3.y = (int)result.getMouthCenterY()+25;
BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
/*myBitmap=BitmapFactory.decodeFile(pic.getPath());
myBitmap_Circle = BitmapFactory.decodeResource(getResources(), R.drawable.circle);*/
Drawable d=null;
try {
d = drawable_from_url(result.getImgUrl(), "Image Url");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myBitmap=((BitmapDrawable)d).getBitmap();
//myBitmap_Circle = BitmapFactory.decodeResource(getResources(), R.drawable.circle);
// declare each ball with the ColorBall class
colorballs[0] = new ColorBall(context,myBitmap_Circle, point1);
colorballs[1] = new ColorBall(context,myBitmap_Circle, point2);
colorballs[2] = new ColorBall(context,myBitmap_Circle, point3);
width=myBitmap.getWidth();
height=myBitmap.getHeight();
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(myBitmap, 0,0, null);
//draw the balls on the canvas
for (ColorBall ball : colorballs) {
canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
}
/* canvas.drawCircle(result.getEyeRightX(), result.getEyeRightY(), 8, myPaint);
canvas.drawCircle(result.getMouthCenterX(), result.getMouthCenterY(), 8, myPaint);*/
}
// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
balID = 0;
for (ColorBall ball : colorballs) {
// check if inside the bounds of the ball (circle)
// get the center for the ball
int centerX = ball.getX() + 25;
int centerY = ball.getY() + 25;
// calculate the radius from the touch to the center of the ball
double radCircle = Math.sqrt((double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
// if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
if (radCircle < 23){
balID = ball.getID();
break;
}
}
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
if (balID > 0) {
colorballs[balID-1].setX(X-25);
colorballs[balID-1].setY(Y-25);
}
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
invalidate();
return true;
}
Drawable drawable_from_url(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException
{
return Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);
}
}
您的logcat中沒有提及空指針... – JoxTraex 2012-02-24 07:38:12
堆棧跟蹤中的NullPointerException在哪裏? – user370305 2012-02-24 07:38:31
com.TestFace.Face在這裏檢測到一些錯誤 – Triode 2012-02-24 08:10:11