Android addListenerOnButton问题

时间:2014-11-18 15:03:26

标签: java android

我尝试在主页面上创建一个带有5个按钮的应用程序。出于某种原因,ListenerOnButton无效,应用程序将在显示xml按钮布局之前关闭。

这是我的主要活动:



public class MainListActivity extends Activity {

	 ImageButton news;
	 ImageButton weather;
	 ImageButton counter;
	 ImageButton fakoi;
	 ImageButton gyalia;
	 ImageButton uvindex;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);

		addListenerOnButton();

	}

	  public void addListenerOnButton() {
	        news = (ImageButton) findViewById(R.id.news);
	        weather = (ImageButton) findViewById(R.id.weather);
	        fakoi = (ImageButton) findViewById(R.id.fakoi);
	        gyalia = (ImageButton) findViewById(R.id.gyalia);
	        uvindex = (ImageButton) findViewById(R.id.uvindex);
	        
	        news.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View arg0) {

	                Intent intent = new Intent
	                        (getApplicationContext(), MyOrasisNews.class);
	                    startActivity(intent); 
	            }
	        });
	        weather.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View arg0) {

	                Intent intent = new Intent
	                        (getApplicationContext(), MainActivity.class);
	                    startActivity(intent); 
	            }
	        });
	        gyalia.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View arg0) {

	                Intent intent = new Intent
	                        (getApplicationContext(), GyaliaActivity.class);
	                    startActivity(intent); 
	            }
	        });
	       fakoi.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View arg0) {

	                Intent intent = new Intent
	                        (getApplicationContext(), FakoiActivity.class);
	                    startActivity(intent); 
	            }
	        });
	       uvindex.setOnClickListener(new OnClickListener() {

	            @Override
	            public void onClick(View arg0) {

	                Intent intent = new Intent
	                        (getApplicationContext(), UvMainActivity.class);
	                    startActivity(intent); 
	            }
	        });
	    }
}




这是xml文件



<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/db1_root"
     android:background="@drawable/bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
  <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:weightSum="1"
            android:layout_weight="0.5">
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <Button
                    android:id="@+id/news"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/homebg"
                    android:gravity="center|bottom"
                    android:paddingBottom="10dp"
                    android:singleLine="false"
                    android:text="News"
                    android:textSize="18sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/news"
                    android:layout_centerInParent="true"/>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <Button
                    android:id="@+id/weather"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/homebg"
                    android:gravity="center|bottom"
                    android:paddingBottom="10dp"
                    android:singleLine="false"
                    android:text="Weather"
                    android:textSize="18sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/weather"
                    android:layout_centerInParent="true"/>
            </RelativeLayout>
        </TableRow>
 
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:weightSum="1"
            android:layout_weight="0.5">
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <Button
                    android:id="@+id/uvindex"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/homebg"
                    android:gravity="center|bottom"
                    android:paddingBottom="10dp"
                    android:text="Uv Index"
                    android:textSize="18sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/uvindex"
                    android:layout_centerInParent="true"/>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <Button
                    android:id="@+id/fakoi"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/homebg"
                    android:gravity="center|bottom"
                    android:paddingBottom="10dp"
                    android:text="S Fakwn"
                    android:textSize="18sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/sfakon"
                    android:layout_centerInParent="true"/>
            </RelativeLayout>
        </TableRow>
 
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:weightSum="1"
            android:layout_weight="0.5">
            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <Button
                    android:id="@+id/gyalia"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/homebg"
                    android:gravity="center|bottom"
                    android:paddingBottom="10dp"
                    android:text="S gyalion"
                    android:textSize="18sp" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/sgyalion"
                    android:layout_centerInParent="true"/>
            </RelativeLayout>
          
        </TableRow>
 
    </TableLayout>
     </LinearLayout>
&#13;
&#13;
&#13;

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您在Buttons中定义了xml,但是您正试图将它们转换为ImageButton。将ImageButtons的类型更改为Button

答案 1 :(得分:0)

您尝试将Button强制转换为ImageButton。 在XML文件中,将Button更改为ImageButton或在您的活动中将ImageButton更改为Button。