我是android新手,请原谅我的无知。我正在尝试在主要活动中添加一个按钮,单击此按钮会将应用程序转换为摄像机视图。
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
private Button button;
button = (Button) findViewById(R.id.button)*****
private void setupMessageButton() {
Button 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);
}
});
}
}
我有一个问题,它是星号,说明 第1名:(未知课程:'按钮') 第二个:(无效的方法声明)' findViewById' 第3名:(未知类R.id.button)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id= "@+id/rootRL"
android:orientation="vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:background = "@drawable/background">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp" />
</RelativeLayout>
我的上一个问题已解决,解决了我的问题后,更新了代码。
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;
}
}
修复第一个问题,然后点击我的相机按钮,我的应用程序崩溃了,日志猫说'#34;没有零参数构造函数&#34;。有谁知道如何解决这个问题?我被卡住了。这是我的CameraView1代码:
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);
}
}
}
这是我的Log Cat从崩溃开始:
--------- 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)
有谁知道如何解决这个问题?我被卡住了。
答案 0 :(得分:0)
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;
}
}