java在单击时切换到不同的活动
我是Android新手,目前正在探索它。 我有两个图像按钮,必须在单击时加载不同的活动
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);
}
};
//我的舱单
<activity 安卓:name="myApp" 安卓:label="@string/app_name">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity 安卓:name=".HelloWorld1"></activity>
<activity 安卓:name=".HelloWorld2"></activity>
//还有我的主菜。xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
安卓:id="@+id/widget34"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:background="#ffffff"
>
<GridView
安卓:id="@+id/widget36"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:numColumns="2"
安卓:layout_x="110px"
安卓:layout_y="32px"
安卓:layout_centerInParent="true">
</GridView>
<ImageButton
安卓:id="@+id/timetable"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_x="210px"
安卓:layout_y="142px"
安卓:background="@drawable/icon2">
</ImageButton>
<ImageButton
安卓:id="@+id/location"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_x="100px"
安卓:layout_y="342px"
安卓:background="@drawable/icon">
</ImageButton>
这个代码会导致错误,请任何人指出我的错误所在。 非常感谢
# 1 楼答案
您正在调用
startActivityForResult()
,然后立即调用finish()
。如果活动完成,结果会如何你想要什么样的行为,你得到的是什么。你越具体,你得到的帮助质量就越好
# 2 楼答案
让我们把finish()方法放在一边,因为我不知道它到底在做什么:) 案例1:仔细查看活动xml视图文件,您可能会意外地将按钮定义为按钮而不是ImageButton->;错误
案例2:不要使用
view.getContext()
,而是使用getBaseContext()
或getApplicationContext()
# 3 楼答案
试着写一些类似下面的代码。您还可以定义第一个函数,该函数将在按下按钮时创建新活动
}