【问题标题】:Android onclicklistener at different layouts, i keep failing不同布局的Android onclicklistener,我一直失败
【发布时间】:2012-07-02 21:05:19
【问题描述】:

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

我的设置是: 在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>

这是我的日志:

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'

请帮帮我。

【问题讨论】:

    标签: java android layout onclicklistener


    【解决方案1】:

    试试这个...

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

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

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

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

    【讨论】:

      【解决方案2】:

      查看我编辑的代码:

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

      希望对你有所帮助..

      【讨论】:

      • 非常感谢您的回答,但它仍然不起作用.. :(
      • 您在单击 Activity 中的按钮时设置另一个屏幕。如果这样做,则不允许返回到前一个屏幕。通常使用创建 Intent 并调用另一个 Activity 类来完成。
      • 我会尝试重写以让另一个类来做这件事。我想只运行另一个活动类,并在该类中运行“setContentView”,并在那里制作 onclicklistener 会很聪明。 . 我会告诉你进展如何,非常感谢。
      • 检查您的 xml 文件 main.xml 和 chioverview.xml。检查您是否将按钮声明为 ImageButton 而不是 Buttons
      • 谢谢,我尝试在 xml 和活动中将其设为按钮,现在我收到错误:06-29 06:00:51.966: E/AndroidRuntime(1192): java.lang.RuntimeException:无法启动活动 ComponentInfo{feldballe.FPConsult/feldballe.FPConsult.ChIdentifierActivity}: java.lang.NullPointerException 这可能是因为我没有使用意图吗?
      【解决方案3】:

      图像按钮图像按钮;

      ImageButton imageButton2;

      ImageButton imageButton3;

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

      【讨论】:

        【解决方案4】:

        这是你的问题

        您的 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" />
        

        因此,类转换异常:-)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-01
          相关资源
          最近更新 更多