开发者

Why do I get this error message when I press my button?

开发者 https://www.devze.com 2023-03-17 18:30 出处:网络
I have defined a button in my layout xml file: <Button android:id=\"@+id/my_btn\" android:layout_width=\"30dip\"

I have defined a button in my layout xml file:

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

In my activity class I handle the click event with the following code:

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

I run my application, when I press the button, I got the following error message from Eclipse LogCat console:

Why do I get this error message when I press my button?

I do not understand what does this error message mean? What does it complain about? Where am I wrong?

(Please mouse right click on the following image, and view image)

If you got problem of viewing the above image , I write the error message here below:

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
...
...

P.S. I got this error after I change the button size and background drawable, sounds odd...I feel very strange too.

-----UPDATE--------

layout file:

<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="@+i开发者_如何学编程d/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"
                        />
...
...


Toast.makeText(ClassName.this, "button clicked", Toast.LENGTH_SHORT).show();

please try it


Your error is in this line....

android:textSize="10dip"

The textSize must be specified in sp instead of dip.replace your code with->

android:textSize="10sp" and you are done!


Try this ::

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

精彩评论

暂无评论...
验证码 换一张
取 消