2015-04-04 92 views
-1

我在Android上實現相機應用程序,我想要一個按鈕,它將引導用戶到開發人員網站或我的網站。從相機上的按鈕調用另一個活動Android

此按鈕位於快門按鈕的右側。

我試圖從此按鈕調用webview活動,但它給了我錯誤,我對這種情況感到困惑,因爲有很多示例調用另一個活動內的活動,但沒有來自相機應用程序。

我不知道我做錯了什麼。

下面是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.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) 

世界按鈕的佈局聲明是剛剛好,我不我真的認爲這是因爲這個原因,我想這個相機方法裏面有些東西不允許我正確地打電話。

誰能一些線索呢?

在此先感謝!

編輯

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> 
+0

可以顯示主活動的佈局文件? – 2015-04-04 05:03:51

+0

明確的例外表明您嘗試使用按鈕 – 2015-04-04 05:04:57

+0

投射圖像視圖Hi Bhavdip,謝謝,添加了主要活動 – NeoVe 2015-04-04 05:07:15

回答

2

您好我有更新您的活動類,只需更換我的代碼讓我知道你仍然面臨的問題。

​​

謝謝

+0

不,謝謝你隊友!大聲笑,超級真棒,要尋找變化,更好地瞭解爲什麼錯誤,非常感謝你! – NeoVe 2015-04-04 05:18:07

+0

歡迎你。嘗試識別日誌貓中的例外情況。 – 2015-04-04 05:22:27

+0

哈哈我現在明白了,謝謝。 – NeoVe 2015-04-04 05:22:33

相關問題