我正在创建一个Android应用程序,我不会从" test1"到" test2"但是当我按下" btnNext"什么都没发生。我在我的应用程序中使用的代码与我用于其他导航的代码相同,所以我不明白为什么它不能正常工作。有人可以帮忙吗?
" TEST1" 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>
&#34; Test1.java&#34;代码:
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);
}
}
&#34; Test2.java&#34;代码:
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中声明了这些活动,并且我的log cat中出现了零错误。
答案 0 :(得分:1)
您有两个名为“btnNext”的按钮。这就是为什么它不起作用。
仔细检查一下:
"@+id/btnNext"
您设置了两次相同的ID ...
答案 1 :(得分:0)
将xml中的按钮代码更改为此
<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/btnNext1"
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" />
您遇到问题,因为您正在拨打错误ID的按钮。检查test2.xm中的按钮ID。它会运行