Java Android - ImageButton 显示但不允许我单击它

标签 java android image button imagebutton

我从一个具有 surfaceView 的类创建了“runGraphics.java”类的新实例。当它打开此类时,现在在放入 setContentView 时会遇到问题。在调用 runGraphics 类之前在不同的函数中调用它之前。 这是我的 Activity :

  import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.Toast;

    public class runGraphics extends Activity 
    {
        ImageButton polarCap1;

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState); 
        setContentView(R.layout.graphics);
        polarCap1 = (ImageButton) findViewById(R.id.polarCapButton);
        polarCap1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {   
                Toast.makeText(runGraphics.this, "IT WORKED", Toast.LENGTH_LONG).show();
            }//end onClick function         
        });//end setOnClickListener


        }//end onCreate function
    }//end runGraphics class

这是我的 xml:

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

    <ImageButton 
        android:id="@+id/polarCapButton"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/polarcap" />

</LinearLayout>

这是我添加 setContentView 后的 logcat:

05-21 16:52:38.726: E/AndroidRuntime(18959): FATAL EXCEPTION: Thread-17
05-21 16:52:38.726: E/AndroidRuntime(18959): java.lang.RuntimeException: Can't create handler    inside thread that has not called Looper.prepare()
05-21 16:52:38.726: E/AndroidRuntime(18959):    at android.os.Handler.<init>(Handler.java:121)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at android.app.Activity.<init>(Activity.java:704)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at com.twentytwentythree.sab.runGraphics.<init>(runGraphics.java:11)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at com.twentytwentythree.sab.GraphicsSurface$SetupGraphicsSurface.run(GraphicsSurface.java:280)
05-21 16:52:38.726: E/AndroidRuntime(18959):    at java.lang.Thread.run(Thread.java:1019)

最佳答案

public class runGraphics extends Activity {
    ImageButton polarCap1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout.xml);//You need this code line.

        polarCap1 = (ImageButton) findViewById(R.id.polarCapButton);
        polarCap1.setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                        Toast.makeText(runGraphics.this, "IT WORKED",
                        Toast.LENGTH_LONG).show();
               }//end onClick function         
        });//end setOnClickListener


    }//end onCreate function
}//end runGraphics class

关于Java Android - ImageButton 显示但不允许我单击它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23793537/

相关文章:

java - 如何更好地将丰富的字符串内容和图像保存到数据库中?

java - 使用 animateLayoutChanges 调整 EditText 大小时光标位于错误位置

javascript - Android - 仅顺时针旋转 View 180 度

java - 将投影添加到吗啡查询

android - 应用程序配置错误对不起,myapp未获批准在App Center中显示。在android app share中

android - 改变 ActionBar 的方向

image - 函数calcHist()给我们带来了什么

iphone - 将标题和图像添加到导航栏

java - 在java中搜索文本文件中的字符串时出现问题

java - 在 Jython 中将字符串包装在 File 对象中