当前位置: 首页 > 知识库问答 >
问题:

Intellij Idea和Eclipse以及JDK 7和Android

白驰
2023-03-14

我开始使用Intellij Idea为Android开发应用程序,因为它是去年的首选IDE。两个IDE:Intellij Idea 1的致命错误让我大吃一惊。我创建了自定义视图:

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设计器中看到结果:

<?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>
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)
    null

更新的答案:因为我的名声很小,所以我不能回答我自己的问题,所以回答问题:我的自定义视图在com.example.customview.ImageCustomView包中。当我尝试在其他项目中使用它时,我把它放在com.example中,它工作起来,然后我重构了这个视图,命名为ImageCustomView,把它移到com.example中,它工作起来也很好。我试着重复我的错误--但没有成功,在所有的包中,用不同的名字,它的工作。谢谢!

更新了更新了:我重复我的bug。我添加了Geasture监听器,删除了带有AttributeSet的构造函数,又得到了NPE。但当我放弃所有更改时-NPE不会消失。所以我现在不知道什么是错的,所以我需要一些版本或答案。我再次修复它:我将自定义视图类移动到其他包中。

更多信息:当我移除构造函数的on时出现NPE:

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

    }
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中出现...可能是关键信息。我的目标是在屏幕上移动图像和重绘图像,我的手指放置图像。

共有1个答案

阎枫涟
2023-03-14

你的代码对我来说很好(IntelliJ IDEA又名Android Studio)。仔细检查您的项目设置。

 类似资料:
  • 本文向大家介绍==和===、以及Object.is的区别?相关面试题,主要包含被问及==和===、以及Object.is的区别?时的应答技巧和注意事项,需要的朋友参考一下 参考回答: (1) == 主要存在:强制转换成number,null==undefined " "==0 //true "0"==0 //true " " !="0" //true 123=="123" //true null=

  • 我一直在研究JDK 7中的新特性,即Fork和Join。在Javadoc中,它指出,在中,不能仅在的上下文中执行fork。但是它没有提到方法调用是否创建了一个新线程。 使用工作窃取算法来平衡线程之间的工作,但没有提到实际创建了多少线程。 我有一个任务,我应该以某种方式分解它,但我担心会创建太多线程,并且会因为管理这些线程的开销而降低执行性能。 有人能帮我吗?

  • 问题内容: 我在尝试运行Android应用程序时遇到问题,直到向其构建路径添加第二个外部库为止,该应用程序都运行良好。由于添加了scoreninja jar,当我尝试运行该应用程序时,现在出现了NoClassDefFoundError。 这是消息: 因为所有的构建脚本都是由Android工具生成的(?),所以我不确定除清理,重建或重新启动Eclipse外我还能做些什么(我已经尝试了全部三个)。有人

  • 我尝试了一些具有分析功能的示例,并创建了一个sql fiddle来理解分区上的count distinct by Clause,这就是我的SQLFiddle。 如果查看结果集,我会认为第三行的valcount为1,但它是2,不确定为什么会这样。

  • 主要内容:1 HashMap的概述,2 主要类属性,3 主要内部类,3.1 Node,3.2 TreeNode,4 构造器,4.1 HashMap(),4.2 HashMap(initialCapacity),4.3 HashMap(initialCapacity loadFactor),4.4 HashMap(m),5 put方法,5.1 顶层put方法,5.2 putVal插入键值对,5.3. put方法流程图总结,,,,,,,,,,,,,,,基于JDK1.8对HashMap集合的主要方法源

  • 使用AtomicLong.IncrementAndGet方法测试JDK7和JDK8的性能差异,测试数据表明JDK7的性能优于JDK8。为什么JDK7的性能比JDK8好?是什么导致JDK8中的性能差? 系统环境: CPU:Intel(R)至强(R)CPU E5620@2.40 GHz 2.40 GHz(双处理器) 内存:8.00 GB set jvm_opt=-xms1024m-xmx1024m-x