0

我已经在互联网上彻底搜索了答案,并尝试了不同的设置,但我就是无法让它工作。问题是,我不完全确定要搜索什么,以及如何正确地制定它,无论如何,我的问题是:

我的设置是:在 Eclipse 中为 android 2.1 开发。

我想在不同的布局中添加一个 onclicklistener。我有 2 种不同的布局,main.xml 和 ChiOverview.xml。我必须在 main.xml 文件中设置按钮(imagebutton1 和 imagebutton2),以及 4 个按钮(其中一个是 ChIdentifierbutton)。当为 ChIdentifierbutton 制作 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>

这是我的日志:

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'

请帮帮我。

4

4 回答 4

1

尝试这个...

当我跟踪您的错误日志时..我发现了这个

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

在布局中使用了 Button,并且正在将其转换为类中的 ImageButton

在你的布局中使用 ImageButton,或者在你的类中使用 Button 来使它工作......

于 2012-06-29T05:01:53.427 回答
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);

        }

    });

}
}

希望这可以帮助你..

于 2012-06-29T04:28:33.397 回答
0

这是你的问题

您的 findViewById() 函数会查找 ID 为“R.id.ChIdentifierButton”的 ImageButton

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

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

当您将“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" />

因此,类转换异常:-)

于 2012-07-02T13:05:19.047 回答
0

图像按钮图像按钮;

图像按钮 imageButton2;

图像按钮 imageButton3;

根据您的日志,您可能正在将 Button 转换为 ImageButton。您可以粘贴 main.xml 的代码吗?

于 2012-06-29T05:27:02.213 回答