android - 不存在这样的方法

标签 android nosuchmethoderror

我一直在研究我在 Android Development - Playing with graphics in Android 找到的代码.

不幸的是,我没有那么幸运地实现了我认为基于 LogCat 的代码,Android 无法找到确实存在的方法。

以下是 logcat 的详细信息:

Logcat

 02-08 12:43:07.725: E/AndroidRuntime(9284): FATAL EXCEPTION: main
    02-08 12:43:07.725: E/AndroidRuntime(9284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.odhranlynch.interiordesigner/com.odhranlynch.interiordesigner.NewProduct}: android.view.InflateException: Binary XML file line #6: Error inflating class com.odhranlynch.interiordesigner.NewProduct$designCanvas
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.os.Handler.dispatchMessage(Handler.java:99)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.os.Looper.loop(Looper.java:123)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.ActivityThread.main(ActivityThread.java:3691)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at java.lang.reflect.Method.invokeNative(Native Method)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at java.lang.reflect.Method.invoke(Method.java:507)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at dalvik.system.NativeStart.main(Native Method)
    02-08 12:43:07.725: E/AndroidRuntime(9284): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.odhranlynch.interiordesigner.NewProduct$designCanvas
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.view.LayoutInflater.createView(LayoutInflater.java:508)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.Activity.setContentView(Activity.java:1663)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at com.odhranlynch.interiordesigner.NewProduct.onCreate(NewProduct.java:28)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    02-08 12:43:07.725: E/AndroidRuntime(9284):     ... 11 more
    02-08 12:43:07.725: E/AndroidRuntime(9284): Caused by: java.lang.NoSuchMethodException: designCanvas(Context,AttributeSet)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at java.lang.Class.getMatchingConstructor(Class.java:643)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at java.lang.Class.getConstructor(Class.java:472)
    02-08 12:43:07.725: E/AndroidRuntime(9284): at android.view.LayoutInflater.createView(LayoutInflater.java:480)
02-08 12:43:07.725: E/AndroidRuntime(9284):     ... 21 more

NewProduct.java

public class NewProduct extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.new_product);
}

class designCanvas extends SurfaceView implements SurfaceHolder.Callback {
    private drawThread _thread;
    private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();

    public designCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
        _thread = new drawThread(getHolder(), this); 
        setFocusable(true);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        synchronized (_thread.getSurfaceHolder()) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.circle_point));
                graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
                graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
                _graphics.add(graphic);
            }
            return true;
        }
    }
    
    @Override 
    public void onDraw(Canvas canvas) {
        //Set canvas background size and colour
        Path mPath = new Path();
        Paint paint = new Paint(); 
        canvas.drawColor(Color.DKGRAY);
        Bitmap b = Bitmap.createBitmap(200, 200,
        Bitmap.Config.ARGB_8888);
        

        paint.setFilterBitmap(true);
        Bitmap productImage = BitmapFactory.decodeResource(getResources(),R.drawable.glass_lamp);
        canvas.drawBitmap(productImage, 0, 0, paint);

        Canvas c = new Canvas(b);
        paint.setAlpha(255); //0x80
        c.translate(0, 30);
        //c.drawBitmap(productImage, new Matrix(), paint);
        paint.setColor(Color.BLUE);
        
        Bitmap bitmap;
        GraphicObject.Coordinates coords;
        for (GraphicObject graphic : _graphics) {
            //Create a graphical point indicating where the user has touched on screen.
            bitmap = graphic.getGraphic();
            coords = graphic.getCoordinates();
            canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
            //Create path coords points are being placed on canvas.
            Log.d("Point Cords", String.valueOf(coords.getX() + " " + coords.getY()));
            
            if (_graphics.isEmpty()) {
                //_graphics array has no entries, so this is the first coord.
                mPath.moveTo(coords.getX(), coords.getY());
            } else {
                //Record subsequent coords.
                mPath.lineTo(coords.getX(), coords.getY());
            }
        }
    }
    
    //@Override
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //TODO something
    }
    
    //@Override
    public void surfaceCreated(SurfaceHolder holder) {
        _thread.setRunning(true);
        _thread.start();
    }
 
    //@Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // simply copied from sample application LunarLander:
        // we have to tell thread to shut down & wait for it to finish, or else
        // it might touch the Surface after we return and explode
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        } 
    }
}

class drawThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private designCanvas _panel;
    private boolean _run = false;
        
    public drawThread(SurfaceHolder surfaceHolder, designCanvas panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }
        
    public void setRunning(boolean run) {
        _run = run;
    }
        
    public SurfaceHolder getSurfaceHolder() {
        return _surfaceHolder;
    }
        
    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) { 
                    _panel.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

class GraphicObject {
    /**
     * Contains the coordinates of the graphic.
     */
    public class Coordinates {
        private int _x = 100;
        private int _y = 0;
 
        public int getX() {
            return _x + _bitmap.getWidth() / 2;
        }
 
        public void setX(int value) {
            _x = value - _bitmap.getWidth() / 2;
        }
 
        public int getY() {
            return _y + _bitmap.getHeight() / 2;
        }
 
        public void setY(int value) {
            _y = value - _bitmap.getHeight() / 2;
        }
 
        public String toString() {
            return "Coordinates: (" + _x + "/" + _y + ")";
        }
    }
    
    private Bitmap _bitmap;
    private Coordinates _coordinates;

    public GraphicObject(Bitmap bitmap) {
        _bitmap = bitmap;
        _coordinates = new Coordinates();
    }

    public Bitmap getGraphic() {
        return _bitmap;
    }

    public Coordinates getCoordinates() {
        return _coordinates;
    }
}

最后,new_product.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >

<view class="com.odhranlynch.interiordesigner.NewProduct$designCanvas"
    android:id="@+id/surfaceView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true" />

             ... Code snipped ...

</RelativeLayout>

对于上面的代码量,我很抱歉,我只是想知道我哪里错了。 非常感谢:)

最佳答案

当然这里有有用的post ofcourse Error inflating inner class view

最后的帮助是改变 在布局 xml 中 <view class=<View class= 因为布局 XML 是 case sensitive .

此处摘录

http://developer.android.com/guide/topics/ui/declaring-layout.html

通常,用于声明 UI 元素的 XML 词汇表与类和方法的结构和命名密切相关,其中元素名称对应类名称,属性名称对应方法。事实上,这种对应关系通常非常直接,以至于您可以猜出哪个 XML 属性对应于一个类方法,或者猜出哪个类对应于给定的 xml 元素。 但是请注意,并非所有词汇都是相同的。在某些情况下,存在细微的命名差异。例如,EditText 元素具有对应于 EditText.setText() 的文本属性。

关于android - 不存在这样的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9193861/

相关文章:

java - PermissionUtils 无法解决

android - Fragment注入(inject)时查看ClassCastException

android - 具有透明背景的自定义 ActionBar

android - 使用 robolectric 进行单元测试会出现 NoSuchMethodException 错误

java - 旧安卓设备上 ContentValues.keySet() 中的 NoSuchMethod 错误

Android 页面/边缘检测和转换页面。转换为黑白 tiff

android - 在 Kotlin 中使用 @Parcelize 注释的 NoSuchMethodError

java - 如何修复 NoSuchMethodError?

java - 解释 java.lang.NoSuchMethodError 消息

android - .so 文件有意外的 e_machine : 62 (EM_X86_64)