[英]Switching between layout in android
public class menu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_page);
{
Button but1 = (Button)findViewById(R.id.imageButton4);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.indore.indoreindicator.Busone"));
}
});
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
我的菜单类中有这行代码
商船级-
package com.indore.indoreindicator;
import android.app.Activity;
import android.os.Bundle;
public class Busone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.busmenu);
}
}
Android清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.indore.indoreindicator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.indore.indoreindicator.MainActivityPage"
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=".menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Busone"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.indore.indoreindicaotr.Busone" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
主要活动页面-
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="47dp"
android:layout_toLeftOf="@+id/TextView05"
android:src="@drawable/bus"
tools:ignore="ContentDescription" />
我的应用程序启动,但在启动屏幕后失败,无法在第一页上移动。 但是,如果我评论按钮编码部分,则应用程序移至菜单页面。
为什么我不能从菜单页面转到名为bus menu的第二页?
你应该尝试使用
startActivity(new Intent(getApplicationContext(), Busone.class);
代替
startActivity(new Intent("com.indore.indoreindicator.Busone"));
如果您只需要在布局之间切换,则只需添加setOnClickListener
setContentView(R.layout.whatevername);
然后在这之后简单地添加你的布局东西,如imageview,button等。 仅当您需要为该布局特定的子项添加另一个onClick时才这样做! 如果您想在活动“班级”之间切换
Intent intent = new Intent(getApplication(), SecondClass.class);
startActivity(intent);
并且不要忘记在android清单中添加其他活动
<activity android:name=".SecondClass"/>
根据您的CODE
看到编辑!
public class menu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_page);
**{** // why this is here ?????
Button but1 = (Button)findViewById(R.id.imageButton4);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.indore.indoreindicator.Busone"));
}
});
**}** // why this is here ????
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
尝试这个:
startActivity(new Intent(menu.this, Busone.class));
我不理解您要解释的内容,但是我看到的是您正在xml布局中使用图像按钮,并在菜单类中使用普通按钮。 无需使用图像按钮,只需使用android:background将图像设置为“常规”按钮即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.