[英]Intellij Idea and Eclipse and JDK 7 and Android
我开始使用Intellij Idea开发适用于Android的应用程序,因为去年它是首选的IDE。 我在两个IDE上都遇到了两个致命错误,这使我震惊:Intellij Idea1。我创建了自定义视图:
public class ImageOverlayView extends View implements View.OnTouchListener {
private static final String TAG = "ImageOverlayView";
private Bitmap image;
private float x, y, sX, sY;
private final Paint paint = new Paint();
private float scaleFactor;
private int getScaledWidth()
{
return (int)(image.getWidth() * scaleFactor);
}
private int getScaledHeight()
{
return (int)(image.getHeight() * scaleFactor);
}
public ImageOverlayView(Context context) {
super(context);
}
public ImageOverlayView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(R.styleable.ImageOverlayView);
this.setOnTouchListener(this);
}
public void setImage(Bitmap bm) {
image = bm;
invalidate();
}
public void setDrawable(Drawable drawable) {
image = BitmapUtils.drawableToBitmap(drawable);
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (canvas != null && image != null) {
canvas.drawBitmap(image, null, paint);
} else {
Log.d(TAG, "Canvas is NULL");
}
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
sX = event.getX();
sY = event.getY();
break;
}
}
return super.onTouchEvent(event);
}
public void loadImageBitmap(String fileName) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
image = BitmapFactory.decodeFile(fileName, options);
if (image == null) {
throw new NullPointerException("The image can't be decoded.");
}
scaleFactor = 1;
// center image on the screen
int width = getWidth();
int height = getHeight();
if ((width != 0) || (height != 0)) {
int scrollX = (image.getWidth() < width ? -(width - image.getWidth()) / 2 : image.getWidth() / 2);
int scrollY = (image.getHeight() < height ? -(height - image.getHeight()) / 2 : image.getHeight() / 2);
scrollTo(scrollX, scrollY);
}
invalidate();
}
public void loadImageDrawable(int resourceId) {
Resources resources = getResources();
Drawable drawable = resources.getDrawable(resourceId);
image = BitmapUtils.drawableToBitmap(drawable);
invalidate();
}
}
然后,我试图在Idea中的UI Designer中查看结果:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ru.lookmyway.customview.ImageOverlayView
android:id="@+id/imageOverlayView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这个想法给了我NPE:
java.lang.NullPointerException
at ru.lookmyway.customview.ImageOverlayView.onDraw(ImageOverlayView.java:65)
at android.view.View.draw(View.java:13712)
at android.view.View.draw(View.java:13596)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13594)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13594)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13715)
at android.view.View.draw(View.java:13596)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13715)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:570)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
at org.jetbrains.android.uipreview.RenderService.createRenderSession(RenderService.java:127)
at org.jetbrains.android.uipreview.RenderUtil.renderLayout(RenderUtil.java:154)
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$8.run(AndroidDesignerEditorPanel.java:346)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:289)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
但是同时在Eclipse(JUNO)中一切正常!
然后,我决定使用Eclipse进行开发。 我安装了它并尝试构建我的项目。 但是Eclipse告诉我说我正在使用Java 1.7,而Android则希望是5.0或6.0。 一些谷歌搜索说我不能使用jdk 1.7,因为在Android中尚不支持它,但是Idea在android和jdk 1.7上运行良好!
一些问题:
更新后的答案:因为我的声誉不高,所以我无法回答自己的问题,所以请回答:我的自定义视图位于com.example.customview.ImageCustomView包中。 当我尝试在其他项目中使用它时,我将其放置在com.example中并且可以正常工作,而不是将这个视图重构为ImageCustomView,将其移动到com.example中,然后再次正常工作。 我试图重复我的错误-但在所有程序包中使用不同的名称都无法成功。 谢谢!
更新更新:我重复我的错误。 我添加了Geasture侦听器,使用AttributeSet删除了构造函数,然后又得到了NPE。 但是当我放弃所有更改时-NPE不会消失。 所以我现在不知道怎么了,所以我需要一些版本或答案。 我再次修复它:我将自定义视图类移到其他包中。
更多信息:当我删除构造函数时,出现NPE:
public ImageCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(R.styleable.ImageCustomView);
a.recycle();
}
即使我重新添加它-NPE也不会消失,但是在重构该类的名称后-它可以工作。 因此,我的版本是重构Idea刷新预编译的类之后,因为它是Intellij Idea的功能之一,因此可以预编译文件。
经过一些到期后,我有了新的NPE:
java.lang.NullPointerException
at android.graphics.Canvas.throwIfRecycled(Canvas.java:1025)
at android.graphics.Canvas.drawBitmap(Canvas.java:1065)
at ru.lookmyway.ImageCustomView.onDraw(ImageCustomView.java:63)
at android.view.View.draw(View.java:13712)
at android.view.View.draw(View.java:13596)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13594)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13594)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13715)
at android.view.View.draw(View.java:13596)
at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
at android.view.View.draw(View.java:13715)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:570)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
at org.jetbrains.android.uipreview.RenderService.createRenderSession(RenderService.java:127)
at org.jetbrains.android.uipreview.RenderUtil.renderLayout(RenderUtil.java:154)
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$8.run(AndroidDesignerEditorPanel.java:346)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:289)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
更新的自定义视图:
public class ImageCustomView extends View implements View.OnTouchListener {
private static final String TAG = "ImageCustomView";
private Bitmap image;
private float x, y, sX, sY;
private final Paint paint = new Paint();
private float scaleFactor;
private GestureDetector gestureDetector;
private int getScaledWidth()
{
return (int)(image.getWidth() * scaleFactor);
}
private int getScaledHeight()
{
return (int)(image.getHeight() * scaleFactor);
}
public ImageCustomView(Context context) {
super(context);
this.setOnTouchListener(this);
paint.setFilterBitmap(true);
paint.setDither(false);
gestureDetector = new GestureDetector(context, new MyGestureListener());
}
public void setImage(Bitmap bm) {
image = bm;
invalidate();
}
public void setDrawable(Drawable drawable) {
image = BitmapUtils.drawableToBitmap(drawable);
invalidate();
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawBitmap(image, 0, 0, paint);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
sX = event.getX();
sY = event.getY();
Log.d(TAG, "x: " + sX + " y: " + sY);
break;
}
}
return super.onTouchEvent(event);
}
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
scrollBy((int)distanceX, (int)distanceY);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
int fixedScrollX = 0, fixedScrollY = 0;
int maxScrollX = getScaledWidth(), maxScrollY = getScaledHeight();
if (getScaledWidth() < getWidth())
{
fixedScrollX = -(getWidth() - getScaledWidth()) / 2;
maxScrollX = fixedScrollX + getScaledWidth();
}
if (getScaledHeight() < getHeight())
{
fixedScrollY = -(getHeight() - getScaledHeight()) / 2;
maxScrollY = fixedScrollY + getScaledHeight();
}
boolean scrollBeyondImage = (fixedScrollX < 0) || (fixedScrollX > maxScrollX) || (fixedScrollY < 0) || (fixedScrollY > maxScrollY);
return !scrollBeyondImage;
}
}
public void loadImageBitmap(String fileName) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
image = BitmapFactory.decodeFile(fileName, options);
if (image == null) {
throw new NullPointerException("The image can't be decoded.");
}
scaleFactor = 1;
// center image on the screen
int width = getWidth();
int height = getHeight();
if ((width != 0) || (height != 0)) {
int scrollX = (image.getWidth() < width ? -(width - image.getWidth()) / 2 : image.getWidth() / 2);
int scrollY = (image.getHeight() < height ? -(height - image.getHeight()) / 2 : image.getHeight() / 2);
scrollTo(scrollX, scrollY);
}
invalidate();
}
public void loadImageDrawable(int resourceId) {
Resources resources = getResources();
Drawable drawable = resources.getDrawable(resourceId);
image = BitmapUtils.drawableToBitmap(drawable);
invalidate();
}
}
我删除了第二个构造体-获得了NPE,重构了类的名称-获得了作品,添加了一些更改-获得了NPE(关于画布,第二个NPE)重构了类的名称(或位置)-获得了作品...我不知道...更详细一点-该视图对“触摸我的图像”没有任何反应,断点不会在onTouch方法中使用...可能是关键信息。 我的目标是在屏幕上移动图像并在我用手指放置图像的地方重绘图像。
您的代码对我来说工作正常(IntelliJ IDEA,又名Android Studio)。 仔细检查您的项目设置。
Android Studio保存我的屁股。 它显示了我的错误。 因此,关于Eclipse和JDK 1.7我没有答案,但是关于Custom视图,答案是:
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
onDraw方法必须包含检查整个数据是否为null的方法:
if (bitmap == null) { paint.setStyle(Paint.Style.FILL); paint.setColor(Color.CYAN); canvas.drawRect(mImagePosition, paint); return; } Rect rect = new Rect(0, 0, this.getWidth(), this.getHeight()); canvas.drawBitmap(bitmap, rect, mImagePosition, null);
不需要make:
super.onDraw(canvas);
在onDraw方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.