简体   繁体   English

通过相机Android上的按钮调用其他活动

[英]Call another activity from button on camera Android

I'm implementing a Camera app on Android, I'd like to have a button on it which will lead the user to the developer website, or my website. 我正在Android上实现Camera应用程序,我想在其上有一个按钮,该按钮会将用户引导至开发者网站或我的网站。

This button is placed right of the shutter button. 此按钮位于快门按钮的右侧。

I was trying to call the webview activity from this button, but it's giving me errors and I'm confused on this case, because there are plenty of examples of calling an activity inside another one, but no from a camera app. 我试图通过此按钮调用webview活动,但它给我带来了错误,并且我对这种情况感到困惑,因为有很多在另一个应用程序中调用活动的示例,而在相机应用程序中却没有。

I don't know what am I doing wrong. 我不知道我在做什么错。

Here's the piece of code in MainActivity: 这是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);

        }
    });
}

Everytime I run it it throws me this error: 每次运行它都会引发此错误:

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.access$700(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.access$700(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)

The layout declaration of the world button is just Okay, I don't really think it's because of that, I guess there is something inside that camera method that doesn't allows me to properly make the call. world按钮的布局声明只是好的,我真的不认为是因为这个原因,我想那是在camera方法中存在不允许我正确进行调用的东西。

Can anybody shed some light on this? 有人能对此有所启示吗?

Thanks in advance! 提前致谢!

EDIT 编辑

activiy_main.xml: 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>

Hi I have update your activity class, just replace with my code let me know you still face the problem. 嗨,我已经更新了您的活动类,只需替换为我的代码即可让我知道您仍然会遇到问题。

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);

        }
    });
}

Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM