我需要实现图像编辑器项目。基本上,我需要在输入图像上添加一些图标,文本然后导出到最终图像。
问题是,当我向视图中添加新图像(通过选项菜单)并执行invalidate();
以显示更改时,我会在方法java.lang.NullPointerexception
中获得onDraw()
...
感谢任何帮助。
以下是代码:
PhotoSortrActivity.java
public class PhotoSortrActivity extends Activity {
PhotoSortrView photoSorter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle(R.string.instructions);
photoSorter = new PhotoSortrView(this);
// setContentView(photoSorter);
setContentView(R.layout.foto);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
this.addContentView(photoSorter, params);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.foto, menu);
return true;
/** true -> el menú ya está visible */
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mascara:
// lanzarAcercaDe(null);
Toast toast1 = Toast.makeText(getApplicationContext(),"Toast por defecto", Toast.LENGTH_SHORT);
toast1.show();
// HERE IS THE PROBLEM
photoSorter.addImage(R.drawable.catarina);
break;
}
return true;
/** true -> consumimos el item, no se propaga */
}
PhotoShortView.java
public class PhotoSortrView extends View implements MultiTouchObjectCanvas<MultiTouchEntity> {
//private static final int[] IMAGES = { R.drawable.m74hubble, R.drawable.catarina, R.drawable.tahiti, R.drawable.sunset, R.drawable.lake };
private ArrayList<MultiTouchEntity> mImages = new ArrayList<MultiTouchEntity>();
// --
private MultiTouchController<MultiTouchEntity> multiTouchController =
new MultiTouchController<MultiTouchEntity>(this);
// --
private PointInfo currTouchPoint = new PointInfo();
private boolean mShowDebugInfo = true;
private static final int UI_MODE_ROTATE = 1, UI_MODE_ANISOTROPIC_SCALE = 2;
private int mUIMode = UI_MODE_ROTATE;
// --
private Paint mLinePaintTouchPointCircle = new Paint();
private static final float SCREEN_MARGIN = 100;
private int width, height, displayWidth, displayHeight;
// ---------------------------------------------------------------------------------------------------
public PhotoSortrView(Context context) {
this(context, null);
init(context);
}
public PhotoSortrView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PhotoSortrView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private void init(Context context) {
Resources res = context.getResources();
// THIS CALL WORKS!!
addImage( R.drawable.m74hubble);
mLinePaintTouchPointCircle.setColor(Color.YELLOW);
mLinePaintTouchPointCircle.setStrokeWidth(5);
mLinePaintTouchPointCircle.setStyle(Style.STROKE);
mLinePaintTouchPointCircle.setAntiAlias(true);
//setBackgroundColor(Color.BLACK);
DisplayMetrics metrics = res.getDisplayMetrics();
this.displayWidth = res.getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE ? Math.max(metrics.widthPixels,
metrics.heightPixels) : Math.min(metrics.widthPixels, metrics.heightPixels);
this.displayHeight = res.getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE ? Math.min(metrics.widthPixels,
metrics.heightPixels) : Math.max(metrics.widthPixels, metrics.heightPixels);
}
/** Called by activity's onResume() method to load the images */
public void loadImages(Context context) {
int n = mImages.size();
for (int i = 0; i < n; i++) {
float cx = SCREEN_MARGIN + (float)
(Math.random() * (displayWidth - 2 * SCREEN_MARGIN));
float cy = SCREEN_MARGIN + (float)
(Math.random() * (displayHeight - 2 * SCREEN_MARGIN));
mImages.get(i).load(context, cx, cy);
}
}
// añade una nueva imagen al array
public void addImage(int image){
Resources res = this.getContext().getResources();
mImages.add(new ImageEntity(image, res));
//postInvalidate();
invalidate(); // ERROR
}
/** Called by activity's onPause() method to free memory used for loading the images */
public void unloadImages() {
int n = mImages.size();
for (int i = 0; i < n; i++)
mImages.get(i).unload();
}
// ---------------------------------------------------------------------------------------------------
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int n = mImages.size();
for (int i = 0; i < n; i++)
mImages.get(i).draw(canvas); //NULL POINTER
if (mShowDebugInfo)
drawMultitouchDebugMarks(canvas);
}
。 。
logcat的
10-12 20:20:20.949: E/AndroidRuntime(16798): FATAL EXCEPTION: main
10-12 20:20:20.949: E/AndroidRuntime(16798): java.lang.NullPointerException
10-12 20:20:20.949: E/AndroidRuntime(16798): at org.android.eboliza_esta_foto.utils.controller.ImageEntity.draw(ImageEntity.java:59)
10-12 20:20:20.949: E/AndroidRuntime(16798): at org.android.eboliza_esta_foto.PhotoSortrView.onDraw(PhotoSortrView.java:137)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.View.draw(View.java:13803)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.View.draw(View.java:13687)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewGroup.drawChild(ViewGroup.java:3019)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2883)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.View.draw(View.java:13685)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewGroup.drawChild(ViewGroup.java:3019)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2883)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.View.draw(View.java:13685)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewGroup.drawChild(ViewGroup.java:3019)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2883)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.View.draw(View.java:13806)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.widget.FrameLayout.draw(FrameLayout.java:467)
10-12 20:20:20.949: E/AndroidRuntime(16798): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2239)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2593)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2484)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2340)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2142)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.Choreographer.doFrame(Choreographer.java:548)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.os.Handler.handleCallback(Handler.java:725)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.os.Handler.dispatchMessage(Handler.java:92)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.os.Looper.loop(Looper.java:153)
10-12 20:20:20.949: E/AndroidRuntime(16798): at android.app.ActivityThread.main(ActivityThread.java:5338)
10-12 20:20:20.949: E/AndroidRuntime(16798): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 20:20:20.949: E/AndroidRuntime(16798): at java.lang.reflect.Method.invoke(Method.java:511)
10-12 20:20:20.949: E/AndroidRuntime(16798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-12 20:20:20.949: E/AndroidRuntime(16798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-12 20:20:20.949: E/AndroidRuntime(16798): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
public void addImage(Context context,int image){
Resources res = this.getContext().getResources();
mImages.add(new ImageEntity(image, res));
loadImages(context); // add this
invalidate();
}