我有一个带有两个按钮的主页。 Button1加载Activity2.class(显示地图),Button2加载Login.class(显示登录表单)。
Button1正在运行,但Button2在点击时不执行任何操作。我只是看不出差异,我对两个按钮使用完全相同的代码结构。也许我错过了一个不同的文件?
这个Android的东西对我来说都是新手,希望有人可以帮忙!
这是我的代码(没有错误)
主要活动。
public class AppActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
public void addListenerOnButton2() {
final Context context = this;
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Login.class);
startActivity(intent);
}
});
}
}
这是登录活动,我正试图进入
public class Login extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting default screen to login.xml
setContentView(R.layout.login);
}
}
Login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#ffffff">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Student Number"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginBottom="20dip"
android:singleLine="true"/>
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Password"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:singleLine="true"
android:password="true"/>
<!-- Login button -->
<Button android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Login"/>
<!-- Login Form Ends -->
</LinearLayout>
</RelativeLayout>
</ScrollView>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".App2Activity" >
</activity>
<activity
android:label="@string/app_name"
android:name=".Login" >
</activity>
</application>
</manifest>
答案 0 :(得分:3)
您没有致电addListenerOnButton2();
答案 1 :(得分:0)
在AppActivity类的on createMethod上添加addListenerOnButton2(
),就像你添加一样
addListenerOnButton()
;
public class AppActivity extends Activity
{
Button button;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
addListenerOnButton2();
}
答案 2 :(得分:0)
缺少方法调用addListenerOnButton2()
答案 3 :(得分:0)
将您的代码块更改为以下
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
addListenerOnButton2();// you are not calling this method in your code
}
答案 4 :(得分:0)
您永远不会在onCreate()中(或您发布的代码中的任何其他位置)调用addListenerOnButton2()方法。
答案 5 :(得分:0)
试试以下代码。
public class AppActivity extends Activity {
Button button,button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(AppActivity.this, App2Activity.class);
startActivity(intent);
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(AppActivity.this, Login.class);
startActivity(intent);
}
});
}