简体   繁体   English

Intellij Idea和Eclipse以及JDK 7和Android

[英]Intellij Idea and Eclipse and JDK 7 and Android

I start to develop application for Android using Intellij Idea because it is preferable IDE for last year. 我开始使用Intellij Idea开发适用于Android的应用程序,因为去年它是首选的IDE。 I've smashed my face with two fatal error with both IDE: Intellij Idea 1. I created custom view: 我在两个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();
}
}

Then, i've tried to see result in UI Designer in Idea: 然后,我试图在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>

And idea gives me NPE: 这个想法给了我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)

But at the same time in Eclipse (JUNO) everything is OK! 但是同时在Eclipse(JUNO)中一切正常!

Than, i decided develop using Eclipse. 然后,我决定使用Eclipse进行开发。 I installed it and try to build my project. 我安装了它并尝试构建我的项目。 But Eclipse said me that i'm using java 1.7, and Android wants 5.0 or 6.0. 但是Eclipse告诉我说我正在使用Java 1.7,而Android则希望是5.0或6.0。 Some google search says that i can not use jdk 1.7 cause there are no support it yet in Android, but Idea works fine with android and jdk 1.7!! 一些谷歌搜索说我不能使用jdk 1.7,因为在Android中尚不支持它,但是Idea在android和jdk 1.7上运行良好!

Some questions: 一些问题:

  1. Why my custom view working fine in Eclipse, but Idea gives me NPE? 为什么我的自定义视图在Eclipse中可以正常工作,但是Idea给了我NPE?
  2. Why Eclipse could not work with jdk 1.7, but Idea works fine with jdk 1.7? 为什么Eclipse不能与jdk 1.7一起使用,但是Idea与jdk 1.7一起可以正常工作?
  3. What i have to do? 我该怎么办? :D :d
  4. Which IDE i have to choose? 我必须选择哪个IDE?

UPDATED ANSWER: Because i've got small reputation, i can not answer my self, so answer in question: My custom view was in package com.example.customview.ImageCustomView. 更新后的答案:因为我的声誉不高,所以我无法回答自己的问题,所以请回答:我的自定义视图位于com.example.customview.ImageCustomView包中。 When i try use it in other project, i placed it in com.example and it work, than i refactored this view to name ImageCustomView, moved it ot com.example and again it work fine. 当我尝试在其他项目中使用它时,我将其放置在com.example中并且可以正常工作,而不是将这个视图重构为ImageCustomView,将其移动到com.example中,然后再次正常工作。 I've tried to repeat my bug - but unsuccess, in all packages, with different names it works. 我试图重复我的错误-但在所有程序包中使用不同的名称都无法成功。 Thanks! 谢谢!


UPDATED UPDATED: I repeat my bug. 更新更新:我重复我的错误。 I add Geasture listener, remove constructor with AttributeSet and i got NPE again. 我添加了Geasture侦听器,使用AttributeSet删除了构造函数,然后又得到了NPE。 But when i discard all my changes - NPE wont dissapear. 但是当我放弃所有更改时-NPE不会消失。 So i dont know now what's wrong, so i need some versions or answer. 所以我现在不知道怎么了,所以我需要一些版本或答案。 I fix it again: i move my custom view class in other package. 我再次修复它:我将自定义视图类移到其他包中。


MORE INFORMATION: NPE appear when i removed on of constructor: 更多信息:当我删除构造函数时,出现NPE:

    public ImageCustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(R.styleable.ImageCustomView);
    a.recycle();

    }

And it even if i add it back - NPE wont disappear, but after refactor name of this class - it works. 即使我重新添加它-NPE也不会消失,但是在重构该类的名称后-它可以工作。 So, my version this is after refactor Idea refresh pre-compiled classes, coz pre-compile files it is one of the feature Intellij Idea. 因此,我的版本是重构Idea刷新预编译的类之后,因为它是Intellij Idea的功能之一,因此可以预编译文件。

After some expirements i've got new NPE: 经过一些到期后,我有了新的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)

Updated custom view: 更新的自定义视图:

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();
}

} }

I removed second construcor - got NPE, refactor name of class - got works, added some changes - got NPE (about canvas, second NPE) do refactor name(or place) of class - got works... i dont know... One more detail - this view has no reactions on Touch my image, breakpoint wont go in method onTouch... may be it is key information. 我删除了第二个构造体-获得了NPE,重构了类的名称-获得了作品,添加了一些更改-获得了NPE(关于画布,第二个NPE)重构了类的名称(或位置)-获得了作品...我不知道...更详细一点-该视图对“触摸我的图像”没有任何反应,断点不会在onTouch方法中使用...可能是关键信息。 My target it is move image on screen and redraw image where i place image by my finger. 我的目标是在屏幕上移动图像并在我用手指放置图像的地方重绘图像。

Your code is working fine for me(IntelliJ IDEA aka Android Studio). 您的代码对我来说工作正常(IntelliJ IDEA,又名Android Studio)。 Double check your project setup. 仔细检查您的项目设置。

Android Studio save my ass. Android Studio保存我的屁股。 It shows me errors. 它显示了我的错误。 So, about Eclipse and JDK 1.7 i have no answer, but about Custom view the answer is: 因此,关于Eclipse和JDK 1.7我没有答案,但是关于Custom视图,答案是:

  • Custom View requires two default constructors: 自定义视图需要两个默认的构造函数:
    public CustomView(Context context) {
         super(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
         super(context, attrs);
    }
  • onDraw method have to contain check entire data for null: 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); 
  • There are no need make: 不需要make:

     super.onDraw(canvas); 

    in onDraw method. 在onDraw方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM