2012-06-29 43 views
0

我在網上搜索了一個答案,並嘗試了不同的設置,但我不能讓它工作。事情是,我不完全知道要搜索什麼,以及如何正確制定它,反正,我的問題是:Android onclicklistener在不同的佈局下,我保持失敗

我的設置是: 在eclipse中爲android 2.1開發。

我想添加一個onclicklistener到不同的佈局。 我有2個不同的佈局,main.xml和ChiOverview.xml。 我必須在我的main.xml文件中的按鈕(imagebutton1和imagebutton2)和4個按鈕(其中之一是ChIdentifierbutton)。當爲不同佈局的ChIdentifier按鈕製作onclicklistener時,我的應用程序崩潰。

這裏是我的ChIdentifierActivity,JAVA源代碼:

package feldballe.FPConsult; 

    import android.app.Activity; 
    import android.hardware.Camera; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.FrameLayout; 
    import android.widget.ImageButton; 
    import android.widget.ImageView; 
    import android.widget.SeekBar; 

    public class ChIdentifierActivity extends Activity { 
     /** Called when the activity is first created. */ 
     ImageButton imageButton; 
     ImageButton imageButton2; 
     ImageButton imageButton3; 
     Preview preview; 
     CameraDemo camerademo; 
     SeekBar ChIdentifierBar; 
     ImageView imageViewChi; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      addListenerOnButton(); 
     } 

     public void addListenerOnButton() {   
      imageButton = (ImageButton) findViewById(R.id.imageButton2); 
      imageButton.setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 
        setContentView(R.layout.chioverview); 
       } 
      }); 

      imageButton2 = (ImageButton) findViewById(R.id.imageButton1); 
      imageButton2.setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 
        setContentView(R.layout.yuwrong); 
       } 
      }); 

      imageButton3 = (ImageButton) findViewById(R.id.ChIdentifierButton); 
      imageButton3.setOnClickListener(new OnClickListener() { 

       public void onClick(View chioverview) { 
        setContentView(R.layout.yuwrong); 
       } 
      });  
    } 
} 

我的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="46dp" 
     android:text="@string/pushChi" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:src="@drawable/mainchi" /> 

    <ImageButton 
     android:id="@+id/imageButton2" 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/imageButton1" 
     android:src="@drawable/realchi" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/weucom" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </RelativeLayout> 

我chioverview.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/ChiFunfactsButton" 
      android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:text="@string/ChiFunfacts" /> 

    <Button 
     android:id="@+id/ChiCalButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/ChiFunfactsButton" 
     android:text="@string/ChiCalendar" /> 

    <Button 
     android:id="@+id/ChIdentifierButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/ChiCalButton" 
     android:text="@string/ChIdentifier2" /> 

    <Button 
     android:id="@+id/ChiPhotosButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/ChiFunfactsButton" 
     android:text="@string/ChiPhotos" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/ChIdentifierButton" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="26dp" 
     android:text="@string/WhereYuWannaGo" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </RelativeLayout> 

這裏是我的logcat :

06-29 04:43:23.566: I/dalvikvm(1043): threadid=3: reacting to signal 3 
06-29 04:43:23.837: I/dalvikvm(1043): Wrote stack traces to '/data/anr/traces.txt' 
06-29 04:43:24.096: I/dalvikvm(1043): threadid=3: reacting to signal 3 
06-29 04:43:24.157: I/dalvikvm(1043): Wrote stack traces to '/data/anr/traces.txt' 
06-29 04:43:24.209: D/gralloc_goldfish(1043): Emulator without GPU emulation detected. 
06-29 04:43:32.436: D/AndroidRuntime(1043): Shutting down VM 
06-29 04:43:32.436: W/dalvikvm(1043): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
06-29 04:43:32.446: E/AndroidRuntime(1043): FATAL EXCEPTION: main 
06-29 04:43:32.446: E/AndroidRuntime(1043): java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageButton 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at feldballe.FPConsult.ChIdentifierActivity$1.onClick(ChIdentifierActivity.java:50) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at android.view.View.performClick(View.java:3511) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at android.view.View$PerformClick.run(View.java:14105) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at android.os.Handler.handleCallback(Handler.java:605) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at android.os.Handler.dispatchMessage(Handler.java:92) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at android.os.Looper.loop(Looper.java:137) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at android.app.ActivityThread.main(ActivityThread.java:4424) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
06-29 04:43:32.446: E/AndroidRuntime(1043):  at dalvik.system.NativeStart.main(Native Method) 
06-29 04:43:33.056: I/dalvikvm(1043): threadid=3: reacting to signal 3 
06-29 04:43:33.076: I/dalvikvm(1043): Wrote stack traces to '/data/anr/traces.txt' 

請幫我一把。

回答

0

看看我編輯的代碼:

import android.app.Activity; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.FrameLayout; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.SeekBar; 

public class ChIdentifierActivity extends Activity { 
/** Called when the activity is first created. */ 

ImageButton imageButton; 
ImageButton imageButton2; 
ImageButton imageButton3; 


Preview preview; 
CameraDemo camerademo; 

SeekBar ChIdentifierBar; 
ImageView imageViewChi; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    addListenerOnButton(); 
} 


public void addListenerOnButton() { 

    imageButton = (ImageButton) findViewById(R.id.imageButton2); 

    imageButton.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      setContentView(R.layout.chioverview); 
      imageButton3 = (ImageButton) findViewById(R.id.ChIdentifierButton); 
      imageButton3.setOnClickListener(new OnClickListener() { 

       public void onClick(View chioverview) { 
       setContentView(R.layout.yuwrong); 

      } 

      }); 
     } 

     }); 

    imageButton2 = (ImageButton) findViewById(R.id.imageButton1); 

    imageButton2.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      setContentView(R.layout.yuwrong); 

     } 

    }); 

} 
} 

希望這可以幫助你..

+0

非常感謝您的回答,但它仍然無法正常工作.. :( – TobyFP

+0

可以顯示我在你的logcat的錯誤 – Nishant

+0

你正在設置另一個屏幕點擊一個按鈕在你的活動。如果你這樣做,你不允許返回到前一個屏幕。一般它是使用創建意圖和調用另一個活動類。 – Nishant

1

嘗試......

,而我是你的跟蹤誤差log..i發現這個

java.lang.ClassCastException: android.widget.Button cannot be cast toandroid.widget.ImageButton

你在您的佈局中使用了按鈕,並且您將將其投影到類別中的ImageButton中。

要麼使用的ImageButton在佈局,或使用按鈕在你的類,使其工作...

0

ImageButton imageButton;

ImageButton imageButton2;

ImageButton imageButton3;

ACC你的日誌,你可能鑄造按鈕的ImageButton的。 你可以粘貼main.xml的代碼嗎?

+0

我現在已經添加了兩個xml文件,謝謝 – TobyFP

0

這裏是你的問題

您findViewById()函數查找與ID 「R.id.ChIdentifierButton」

ImageButton imageButton3;  
imageButton3 = (ImageButton) findViewById(R.id.ChIdentifierButton); 
     imageButton3.setOnClickListener(new OnClickListener() { 

      public void onClick(View chioverview) { 
       setContentView(R.layout.yuwrong); 
      } 
     });  

的ImageButton的,而你指定的「R.id.ChIdentifierButton」在你的XML格式的按鈕

<Button 
    android:id="@+id/ChIdentifierButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/ChiCalButton" 
    android:text="@string/ChIdentifier2" /> 

因此,類強制轉換異常:-)