【问题标题】:Why do I get this error message when I press my button?为什么我按下按钮时会收到此错误消息?
【发布时间】:2011-07-07 11:35:30
【问题描述】:

我在布局 xml 文件中定义了一个按钮:

<Button
   android:id="@+id/my_btn" 
   android:layout_width="30dip"
   android:layout_height="20dip" 
   android:text="@string/click_me"
   android:textSize="10dip"
/>

在我的活动类中,我使用以下代码处理点击事件:

 Button myBtn = (Button) findViewById(R.id.my_btn);
 myBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                 showToast();
            }
          });

public void showToast() {
        Context context = getApplicationContext();
        CharSequence text = "button clicked";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
     }

我运行我的应用程序,当我按下按钮时,我从 Eclipse LogCat 控制台收到以下错误消息:

我不明白这个错误信息是什么意思?它抱怨什么?我哪里错了?

(请鼠标右击下图,查看图片)

如果您在查看上图时遇到问题,我会在下面写下错误消息:

ActivityThread | enter process activity msg=120

ActivityThread | exit process activity msg=120

WindowManager  | waitForLastKey: mFinished=true, mLastWin=null

ViewRoot enter | Dispatching touchevent to com.android.internal.policy.impl.PhoneWindow$DecorView@2fde0c80 touchevent action is 0 X=219.54167 Y=505.3675
...
...

附:更改按钮大小和背景可绘制后出现此错误,听起来很奇怪...我也觉得很奇怪。

-----更新--------

布局文件:

<RelativeLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip">

            ...     
        <TextView
        android:id="@+id/upper_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="10sp"
        android:textStyle="bold"/>

    <Button
        android:id="@+id/my_btn"
        android:layout_toRightOf="@id/upper_text1"
        android:layout_alignTop="@id/upper_text2"
        android:layout_width="80dip"
        android:layout_height="20dip"
        android:background="@drawable/btn_bg"
        android:text="@string/click_me"
                        />
...
...

【问题讨论】:

  • 您可以复制/粘贴日志消息吗?图片太小,无法阅读文字。
  • @Dan,右键单击 -> 查看图像。但我没有看到任何错误
  • 只需右键查看即可。它的大小还可以..
  • 我认为你走错路了

标签: android android-layout android-emulator android-widget android-manifest


【解决方案1】:
Toast.makeText(ClassName.this, "button clicked", Toast.LENGTH_SHORT).show();

请尝试一下

【讨论】:

    【解决方案2】:

    你的错误在这一行......

    android:textSize="10dip"

    textSize 必须在 sp 而不是 dip 中指定。将您的代码替换为->

    android:textSize="10sp" 你就完成了!

    【讨论】:

    • @Richa ,你从哪里得到 10dip 错误的线索?我的按钮文本大小实际上是 8dip...
    • 在你的布局部分你只提到了。基本上我们不使用 dip 来设置 textSize 而是使用 sp。
    【解决方案3】:

    试试这个 ::

     Button my_btn; 
     my_btn = (Button) findViewById(R.id.my_btn);
    
    
    
     my_btn.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
    
    Toast.makeText(getApplicationContext(), "button clicked", Toast.LENGTH_SHORT). show();
    }
    

    【讨论】:

    • @ scorpio ,我的 toast 之前可以正常工作,只是在更改更改按钮主题后,我得到了错误
    • @ scorpio ,为什么我必须接受你的回答,它并没有解决我的问题。您可能误解了我之前的评论,我的意思是我的 toast 实现之前是有效的,我的 toast 没有任何问题,所以您的回答不是我当前问题的正确解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    相关资源
    最近更新 更多