通过相机 Android 上的按钮调用另一个 activity

Call another activity from button on camera Android

我正在 Android 上实现一个相机应用程序,我希望在它上面有一个按钮,它将引导用户访问开发者网站或我的网站。

此按钮位于快门按钮右侧。

我试图从这个按钮调用 webview activity,但它给了我错误,我对这种情况感到困惑,因为里面有很多调用 activity 的例子另一个,但不是来自相机应用程序。

我不知道我做错了什么。

这是 MainActivity 中的一段代码:

public class MainActivity extends Activity /**implements OnClickListener**/ {

ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
Button webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context=this;

    webButton = (Button) findViewById(R.id.imageView_world);
    fotoButton = (ImageView) findViewById(R.id.imageView_foto);
    exitButton = (Button) findViewById(R.id.button_exit);
    image = (ImageView) findViewById(R.id.imageView_photo);
    progressLayout = (LinearLayout) findViewById(R.id.progress_layout);

    preview = new Preview(this,
            (SurfaceView) findViewById(R.id.KutCameraFragment));
    FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
    frame.addView(preview);
    preview.setKeepScreenOn(true);
    fotoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                takeFocusedPicture();
            } catch (Exception e) {

            }
            exitButton.setClickable(false);
            fotoButton.setClickable(false);
            webButton.setClickable(true);
            progressLayout.setVisibility(View.VISIBLE);
        }
    });
    webButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
            MainActivity.this.startActivity(myIntent);

        }
    });
}

每次我 运行 它都会抛出这个错误:

4-04 00:33:43.929    4237-4237/com.kut.kutcamera E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kut.kutcamera/com.kut.kutcamera.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.access0(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:175)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
        at com.kut.kutcamera.MainActivity.onCreate(MainActivity.java:56)
        at android.app.Activity.performCreate(Activity.java:5283)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
        at android.app.ActivityThread.access0(ActivityThread.java:150)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
        at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:175)
        at android.app.ActivityThread.main(ActivityThread.java:5279)
        at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

世界按钮的布局声明还可以,我真的不认为这是因为那个,我猜相机方法中有一些东西不允许我正确地进行调用。

任何人都可以阐明这一点吗?

提前致谢!

编辑

activiy_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
 <FrameLayout
    android:id="@+id/preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >
<SurfaceView
    android:id="@+id/KutCameraFragment"
    android:name="com.kut.camera.KutCameraFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<RelativeLayout
    android:id="@+id/rel_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:alpha="1"
        android:background="@android:color/black"
        android:orientation="vertical"
        android:padding="10dp" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textViewReferan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="Photo"
                android:textColor="@android:color/white"
                android:textSize="16sp" />

            <Button
                android:id="@+id/button_exit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:background="@android:color/transparent"
                android:text="Ok"
                android:textColor="#2799CF" />

        </RelativeLayout>

        <LinearLayout
            android:id="@+id/progress_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="gone" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/islem_value_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..." />

</LinearLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:alpha="0.9"
        android:background="@android:color/black"
        android:padding="10dp" >

        <ImageView
            android:id="@+id/imageView_foto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/camera"
            android:contentDescription="1" />

        <ImageView
            android:id="@+id/imageView_photo"
            android:layout_width="80dp"
            android:layout_height="100dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:padding="5dp"
            android:scaleType="fitCenter"
            android:src="@drawable/fotocekicon"
            android:contentDescription="2" />

        <ImageView
            android:id="@+id/imageView_world"
            android:layout_width="80dp"
            android:layout_height="100dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:padding="5dp"
            android:scaleType="fitCenter"
            android:src="@drawable/world"
            android:contentDescription="2" />

    </RelativeLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mark3"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
</FrameLayout>

</FrameLayout>

您好,我已经更新了您的 activity class,只需替换为我的代码,让我知道您仍然遇到问题。

public class MainActivity extends Activity /**implements OnClickListener**/ {

ImageView image;
Activity context;
Preview preview;
Camera camera;
Button exitButton;
ImageView fotoButton;
ImageView webButton;
LinearLayout progressLayout;
String path = "/sdcard/KutCamera/cache/images/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context=this;

    webButton = (ImageView) findViewById(R.id.imageView_world);
    fotoButton = (ImageView) findViewById(R.id.imageView_foto);
    exitButton = (Button) findViewById(R.id.button_exit);
    image = (ImageView) findViewById(R.id.imageView_photo);
    progressLayout = (LinearLayout) findViewById(R.id.progress_layout);

    preview = new Preview(this,
            (SurfaceView) findViewById(R.id.KutCameraFragment));
    FrameLayout frame = (FrameLayout) findViewById(R.id.preview);
    frame.addView(preview);
    preview.setKeepScreenOn(true);
    fotoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                takeFocusedPicture();
            } catch (Exception e) {

            }
            exitButton.setClickable(false);
            fotoButton.setClickable(false);
            webButton.setClickable(true);
            progressLayout.setVisibility(View.VISIBLE);
        }
    });
    webButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(MainActivity.this, WebActivity.class);
            MainActivity.this.startActivity(myIntent);

        }
    });
}

谢谢