gpt4 book ai didi

java - Android addListenerOnButton 问题

转载 作者:行者123 更新时间:2023-11-30 02:33:13 26 4
gpt4 key购买 nike

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

这是我的 MainActivity:

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>

我们将不胜感激。

最佳答案

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

关于java - Android addListenerOnButton 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26997410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com