我正在创建一个android应用程序,我不会从“test1”导航到“test2”,但是当我按下“btnNext”时,什么都不会发生。我使用与我的应用程序中用于其他导航的代码相同的代码,因此我不明白为什么它不起作用。有人可以帮忙吗?应用程序不会导航到下一个应用程序页面
“测试1” XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/txtSubTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_centerHorizontal="true"
android:text="Please Answer the 9 Following Questions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="Depression Test"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/txtQ1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtSubTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:text="Q.1. Have you found little pleasure or interest in doing things?"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioButton
android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtQ1"
android:layout_below="@+id/radioButton1"
android:text="On some days" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtQ1"
android:layout_below="@+id/txtQ1"
android:text="No, not at all" />
<RadioButton
android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/RadioButton01"
android:layout_below="@+id/RadioButton01"
android:text="On more than half the days" />
<RadioButton
android:id="@+id/RadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/RadioButton02"
android:layout_below="@+id/RadioButton02"
android:text="Nearly every day" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:layout_toRightOf="@+id/txtQ1"
android:text="Next" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/RadioButton03"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/txtTitle"
android:text="Next" />
</RelativeLayout>
“Test1.java” 代码:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//listener call this function
openTest2();
}
});
}
//Open test page
public void openTest2() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test2.class);
startActivity(i);
}
}
“Test2.java” 代码:
package com.lifematters;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class Test2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
//define Navigation Image Buttons
final Button nextBtn = (Button) findViewById(R.id.btnNext);
//Set up listener for Test
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//listener call this function
openTest3();
}
});
}
//Open test page
public void openTest3() {
//create new textview
Intent i = new Intent(getApplicationContext(), Test3.class);
startActivity(i);
}
}
我已经宣布活动在Android Manifest中,我在我的日志猫中获得了零错误。
其中是test2.xml代码? –
但是在两种Java窗体中,我都将实例/ contentView声明为正确的形式,所以我不确定这会有所作为。但我会放弃它。 –
setContentView(R.layout.test2);所以你需要在两种布局中都得到你的按钮名称,然后用各自的名称给他们打电话 –