gpt4 book ai didi

java - 单击切换到不同的 Activity

转载 作者:行者123 更新时间:2023-12-01 15:53:55 25 4
gpt4 key购买 nike

我是 Android 新手,目前正在探索它。我有两个图像按钮,它们必须在单击时加载不同的 Activity 。

ImageButton btn1= (ImageButton)findViewById(R.id.timetable);
btn1.setOnClickListener(btnListener1);

ImageButton btn2= (ImageButton)findViewById(R.id.location);
btn2.setOnClickListener(btnListener2);
private OnClickListener btnListener1 = new OnClickListener()
{
public void onClick(View view)
{
Intent myIntent = new Intent(getBaseContext(), HelloWorld1.class);
startActivity(myIntent);
}
};

private OnClickListener btnListener2 = new OnClickListener()
{
public void onClick(View view)
{
Intent myIntent2 = new Intent(getBaseContext(), HelloWorld2.class);
startActivity(myIntent2);
}
};

//我的 list ......

<activity android:name="myApp" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelloWorld1"></activity>
<activity android:name=".HelloWorld2"></activity>

//和我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget34"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
>

<GridView
android:id="@+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:layout_x="110px"

android:layout_y="32px"
android:layout_centerInParent="true">
</GridView>
<ImageButton
android:id="@+id/timetable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="210px"
android:layout_y="142px"
android:background="@drawable/icon2">
</ImageButton>
<ImageButton
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="342px"
android:background="@drawable/icon">
</ImageButton>

此代码会导致错误,请有人指出我哪里出错了。非常感谢。

最佳答案

让我们把 finish() 方法放在一边,因为我不知道它到底在做什么:)Case1: 仔细查看你的 Activity xml View 文件,你可能会不小心将按钮定义为 Button 而不是 ImageButton -> Error

情况2:不要使用view.getContext(),而是使用getBaseContext()getApplicationContext()

关于java - 单击切换到不同的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5450761/

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