Java 未知类别:';按钮'&;无效方法脱毒;findViewById所需的返回类型

Java 未知类别:';按钮'&;无效方法脱毒;findViewById所需的返回类型,java,android,android-studio,android-button,Java,Android,Android Studio,Android Button,我是android新手,请原谅我的无知。我正试图在我的主要活动中添加一个按钮,单击该按钮将使应用程序转换为摄像头视图 主要活动代码: 我有一个问题,它是星号,声明 第一:(未知类:“按钮”) 第二:(无效的方法声明)用于“findViewById” 第三:(未知类R.id.按钮) activity_main.xml: 我以前的问题解决了,解决了问题后,更新了代码 package com.cerezaenterprises.swiftversionuno; import android.

我是android新手,请原谅我的无知。我正试图在我的主要活动中添加一个按钮,单击该按钮将使应用程序转换为摄像头视图

主要活动代码: 我有一个问题,它是星号,声明 第一:(未知类:“按钮”) 第二:(无效的方法声明)用于“findViewById” 第三:(未知类R.id.按钮)

activity_main.xml:

我以前的问题解决了,解决了问题后,更新了代码

package com.cerezaenterprises.swiftversionuno;


 import android.content.Intent;
 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.Button;



 public class MainActivity extends ActionBarActivity  {
 private Button button;
     @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         button = (Button) findViewById(R.id.button);
         button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, CameraView1.class);
            startActivity(intent);
          }
         });
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is   present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
         return true;
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();

         //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
             return true;
         }

         super.onOptionsItemSelected(item);

         return true;
       }
 }
在解决了第一个问题后,点击我的相机按钮,我的应用程序崩溃了,日志猫说“没有零参数构造函数”。有人知道如何解决这个问题吗??我被卡住了。这是我的照相机W1代码:

package com.cerezaenterprises.swiftversionuno;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import static java.lang.System.loadLibrary;

/**
 * Extention of SurfaceView which starts a camera preview and decode video
 * content on the native side.
 */
public class CameraView1 extends SurfaceView implements SurfaceHolder.Callback,
        Camera.PreviewCallback {
    private static final String TAG = "LiveCamera";


    static {
        loadLibrary("LiveCamera");

    }

    public native void decode(Bitmap pTarget, byte[] pSource);

    private Camera mCamera;
    private byte[] mVideoSource;
    private Bitmap mBackBuffer;
    private Paint mPaint;

    public CameraView1(Context context) {
        super(context);

        // Registers current class so that it listens to surface
        // event (creation, destruction and changes).
        getHolder().addCallback(this);
        // Clears the flag keeping the surface from getting drawn.
        // Necessary when not drawing from a thread.
        setWillNotDraw(false);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            // Acquires the default camera.
            mCamera = Camera.open();
            // Sets landscape mode to avoid complications related to
            // screen orientation handling.
            mCamera.setDisplayOrientation(0);
            // Registers callbacks. Automatic preview is deactivated
            // as we want to process data ourself (in a buffer).
            mCamera.setPreviewDisplay(null);
            mCamera.setPreviewCallbackWithBuffer(this);
        } catch (IOException eIOException) {
            mCamera.release();
            mCamera = null;
            throw new IllegalStateException();
        }
    }

    public void surfaceChanged(SurfaceHolder pHolder, int pFormat, int pWidth,
                               int pHeight) {
        mCamera.stopPreview();

        // Finds a suitable resolution.
        Size lSize = findBestResolution(pWidth, pHeight);

        // Prepares video source and back buffers.
        PixelFormat lPixelFormat = new PixelFormat();
        PixelFormat.getPixelFormatInfo(mCamera.getParameters()
                .getPreviewFormat(), lPixelFormat);
        int lSourceSize = lSize.width * lSize.height
                * lPixelFormat.bitsPerPixel / 8;
        mVideoSource = new byte[lSourceSize];
        mBackBuffer = Bitmap.createBitmap(lSize.width, lSize.height,
                Bitmap.Config.ARGB_8888);

        // Set-up camera size and video format. YCbCr_420_SP
        // should be the default on Android anyway.
        Camera.Parameters lParameters = mCamera.getParameters();
        lParameters.setPreviewSize(lSize.width, lSize.height);
        lParameters.setPreviewFormat(PixelFormat.YCbCr_420_SP);
        mCamera.setParameters(lParameters);

        // Starts receiving pictures from the camera.
        mCamera.addCallbackBuffer(mVideoSource);
        mCamera.startPreview();
    }

    private Size findBestResolution(int pWidth, int pHeight) {
        List<Size> lSizes = mCamera.getParameters().getSupportedPreviewSizes();
        // Finds the biggest resolution which fits the screen.
        // Else, returns the first resolution found.
        Size lSelectedSize = mCamera.new Size(0, 0);
        for (Size lSize : lSizes) {
            if ((lSize.width <= pWidth) && (lSize.height <= pHeight)
                    && (lSize.width >= lSelectedSize.width)
                    && (lSize.height >= lSelectedSize.height)) {
                lSelectedSize = lSize;
            }
        }
        // Previous code assume that there is a preview size smaller
        // than screen size. If not, hopefully the Android API
        // guarantees that at least one preview size is available.
        if ((lSelectedSize.width == 0) || (lSelectedSize.height == 0)) {
            lSelectedSize = lSizes.get(0);
        }
        Log.d(TAG, "findBestResolution: " + lSelectedSize.width + ","
                + lSelectedSize.height);
        return lSelectedSize;
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Releases camera which is a shared resource.
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            // These variables can take a lot of memory. Gets rid of
            // them as fast as we can.
            mCamera = null;
            mVideoSource = null;
            mBackBuffer = null;
        }
    }

    public void onPreviewFrame(byte[] pData, Camera pCamera) {
        // New data has been received from camera. Processes it and
        // requests surface to be redrawn right after.
        decode(mBackBuffer, pData);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas pCanvas) {
        if (mCamera != null) {
            // Draws resulting image at screen origin.
            pCanvas.drawBitmap(mBackBuffer, 0, 0, mPaint);
            // Enqueues buffer again to get next image.
            mCamera.addCallbackBuffer(mVideoSource);
        }
    }
}
package com.cerezaenterprises.swiftVersionNo;
导入java.io.IOException;
导入java.util.List;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.Canvas;
导入android.graphics.Paint;
导入android.graphics.PixelFormat;
导入android.hardware.Camera;
导入android.hardware.Camera.Size;
导入android.util.Log;
导入android.view.SurfaceHolder;
导入android.view.SurfaceView;
导入静态java.lang.System.loadLibrary;
/**
*SurfaceView的扩展,用于启动摄像头预览和解码视频
*本机端的内容。
*/
公共类CameraView1扩展了SurfaceView实现了SurfaceHolder.Callback,
摄影机。预览回调{
私有静态最终字符串标记=“LiveCamera”;
静止的{
loadLibrary(“LiveCamera”);
}
公共本机void解码(位图pTarget,字节[]pSource);
私人摄像机麦卡梅拉;
专用字节[]mVideoSource;
专用位图缓冲区;
私人油漆;
公共摄像W1(背景){
超级(上下文);
//注册当前类,以便它侦听曲面
//事件(创建、销毁和更改)。
getHolder().addCallback(此);
//清除阻止绘制曲面的标志。
//非从螺纹上拔出时必须使用。
setWillNotDraw(假);
}
已创建的公共空白表面(表面持有人){
试一试{
//获取默认摄影机。
mCamera=Camera.open();
//设置横向模式以避免与
//屏幕方向处理。
mCamera.setDisplayOrientation(0);
//注册回调。自动预览已停用
//因为我们希望自己(在缓冲区中)处理数据。
mCamera.setPreviewDisplay(空);
mCamera.setPreviewCallbackWithBuffer(此);
}捕获(IOException eIOException){
mCamera.release();
mCamera=null;
抛出新的非法状态异常();
}
}
公共空隙表面改变(表面厚壁、内部形状、内部宽度、,
内特(菲特){
mCamera.stopPreview();
//找到合适的解决方案。
尺寸lSize=最终解决方案(pWidth,pHeight);
//准备视频源和后台缓冲区。
PixelFormat lPixelFormat=新的PixelFormat();
PixelFormat.getPixelFormatInfo(mCamera.getParameters()
.getPreviewFormat(),lPixelFormat);
int lSourceSize=lSize.width*lSize.height
*lPixelFormat.bitsPerPixel/8;
mVideoSource=新字节[lSourceSize];
mBackBuffer=Bitmap.createBitmap(lSize.width、lSize.height、,
位图.Config.ARGB_8888);
//设置摄像机尺寸和视频格式。YCbCr_420_SP
//应该是Android上的默认设置。
Camera.Parameters lParameters=mCamera.getParameters();
lParameters.setPreviewSize(lSize.width,lSize.height);
lParameters.setPreviewFormat(PixelFormat.YCbCr_420_SP);
设置参数(lParameters);
//开始从相机接收图片。
mCamera.addCallbackBuffer(mVideoSource);
mCamera.startPreview();
}
私有大小的findBestResolution(整数宽,整数宽){
List lSizes=mCamera.getParameters().getSupportedPreviewSizes();
//查找适合屏幕的最大分辨率。
//否则,返回找到的第一个分辨率。
Size lSelectedSize=mCamera.new Size(0,0);
用于(大小:lSizes:lSizes){
如果((lSize.width=lSelectedSize.height)){
lSelectedSize=lSize;
}
}
//以前的代码假设预览大小较小
//如果不是,希望是Android API
//保证至少有一个预览大小可用。
如果((lSelectedSize.width==0)| |(lSelectedSize.height==0)){
lSelectedSize=lSizes.get(0);
}
Log.d(标记“findBestResolution:+lSelectedSize.width+”,“
+l所选尺寸、高度);
返回lSelectedSize;
}
公共空间表面覆盖(表面覆盖物持有人){
//释放作为共享资源的摄影机。
if(mCamera!=null){
mCamera.stopPreview();
mCamera.release();
//这些变量可能会占用大量内存
//我们尽可能快地找到他们。
mCamera=null;
mVideoSource=null;
mBackBuffer=null;
}
}
预览帧上的公共无效(字节[]pData,摄像头pCamera){
//已从摄像机接收到新数据。对其进行处理并
//请求立即重新绘制曲面。
解码(mBackBuffer,pData);
使无效();
}
@凌驾
受保护的void onDraw(帆布pCanvas){
if(mCamera!=null){
//在屏幕原点绘制结果图像。
pCanvas.drawBitmap(mBackBuffer,0,0,mPaint);
//再次将缓冲区排队以获取下一个图像。
麦卡梅拉。
package com.cerezaenterprises.swiftversionuno;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import static java.lang.System.loadLibrary;

/**
 * Extention of SurfaceView which starts a camera preview and decode video
 * content on the native side.
 */
public class CameraView1 extends SurfaceView implements SurfaceHolder.Callback,
        Camera.PreviewCallback {
    private static final String TAG = "LiveCamera";


    static {
        loadLibrary("LiveCamera");

    }

    public native void decode(Bitmap pTarget, byte[] pSource);

    private Camera mCamera;
    private byte[] mVideoSource;
    private Bitmap mBackBuffer;
    private Paint mPaint;

    public CameraView1(Context context) {
        super(context);

        // Registers current class so that it listens to surface
        // event (creation, destruction and changes).
        getHolder().addCallback(this);
        // Clears the flag keeping the surface from getting drawn.
        // Necessary when not drawing from a thread.
        setWillNotDraw(false);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            // Acquires the default camera.
            mCamera = Camera.open();
            // Sets landscape mode to avoid complications related to
            // screen orientation handling.
            mCamera.setDisplayOrientation(0);
            // Registers callbacks. Automatic preview is deactivated
            // as we want to process data ourself (in a buffer).
            mCamera.setPreviewDisplay(null);
            mCamera.setPreviewCallbackWithBuffer(this);
        } catch (IOException eIOException) {
            mCamera.release();
            mCamera = null;
            throw new IllegalStateException();
        }
    }

    public void surfaceChanged(SurfaceHolder pHolder, int pFormat, int pWidth,
                               int pHeight) {
        mCamera.stopPreview();

        // Finds a suitable resolution.
        Size lSize = findBestResolution(pWidth, pHeight);

        // Prepares video source and back buffers.
        PixelFormat lPixelFormat = new PixelFormat();
        PixelFormat.getPixelFormatInfo(mCamera.getParameters()
                .getPreviewFormat(), lPixelFormat);
        int lSourceSize = lSize.width * lSize.height
                * lPixelFormat.bitsPerPixel / 8;
        mVideoSource = new byte[lSourceSize];
        mBackBuffer = Bitmap.createBitmap(lSize.width, lSize.height,
                Bitmap.Config.ARGB_8888);

        // Set-up camera size and video format. YCbCr_420_SP
        // should be the default on Android anyway.
        Camera.Parameters lParameters = mCamera.getParameters();
        lParameters.setPreviewSize(lSize.width, lSize.height);
        lParameters.setPreviewFormat(PixelFormat.YCbCr_420_SP);
        mCamera.setParameters(lParameters);

        // Starts receiving pictures from the camera.
        mCamera.addCallbackBuffer(mVideoSource);
        mCamera.startPreview();
    }

    private Size findBestResolution(int pWidth, int pHeight) {
        List<Size> lSizes = mCamera.getParameters().getSupportedPreviewSizes();
        // Finds the biggest resolution which fits the screen.
        // Else, returns the first resolution found.
        Size lSelectedSize = mCamera.new Size(0, 0);
        for (Size lSize : lSizes) {
            if ((lSize.width <= pWidth) && (lSize.height <= pHeight)
                    && (lSize.width >= lSelectedSize.width)
                    && (lSize.height >= lSelectedSize.height)) {
                lSelectedSize = lSize;
            }
        }
        // Previous code assume that there is a preview size smaller
        // than screen size. If not, hopefully the Android API
        // guarantees that at least one preview size is available.
        if ((lSelectedSize.width == 0) || (lSelectedSize.height == 0)) {
            lSelectedSize = lSizes.get(0);
        }
        Log.d(TAG, "findBestResolution: " + lSelectedSize.width + ","
                + lSelectedSize.height);
        return lSelectedSize;
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Releases camera which is a shared resource.
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            // These variables can take a lot of memory. Gets rid of
            // them as fast as we can.
            mCamera = null;
            mVideoSource = null;
            mBackBuffer = null;
        }
    }

    public void onPreviewFrame(byte[] pData, Camera pCamera) {
        // New data has been received from camera. Processes it and
        // requests surface to be redrawn right after.
        decode(mBackBuffer, pData);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas pCanvas) {
        if (mCamera != null) {
            // Draws resulting image at screen origin.
            pCanvas.drawBitmap(mBackBuffer, 0, 0, mPaint);
            // Enqueues buffer again to get next image.
            mCamera.addCallbackBuffer(mVideoSource);
        }
    }
}
 --------- beginning of crash
07-29 11:33:36.336    2074-2074/com.cerezaenterprises.swiftversionuno E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.cerezaenterprises.swiftversionuno, PID: 2074
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cerezaenterprises.swiftversionuno/com.cerezaenterprises.swiftversionuno.CameraView1}: java.lang.InstantiationException: class com.cerezaenterprises.swiftversionuno.CameraView1 has no zero argument constructor
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.InstantiationException: class com.cerezaenterprises.swiftversionuno.CameraView1 has no zero argument constructor
            at java.lang.Class.newInstance(Class.java:1597)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NoSuchMethodException: <init> []
            at java.lang.Class.getConstructor(Class.java:531)
            at java.lang.Class.getDeclaredConstructor(Class.java:510)
            at java.lang.Class.newInstance(Class.java:1595)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
package com.cerezaenterprises.swiftversionuno;


 import android.content.Intent;
 import android.support.v7.app.ActionBarActivity;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.Button;



 public class MainActivity extends ActionBarActivity  {
 private Button button;
     @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         button = (Button) findViewById(R.id.button);
         button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, CameraView1.class);
            startActivity(intent);
          }
         });
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is   present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
         return true;
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();

         //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
             return true;
         }

         super.onOptionsItemSelected(item);

         return true;
       }
 }