【问题标题】:Application won't navigate to next app page应用程序不会导航到下一个应用程序页面
【发布时间】:2014-02-18 12:26:20
【问题描述】:

我正在创建一个 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>

“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 清单中声明了这些活动,并且我的日志猫中的错误为零。

【问题讨论】:

  • test2.xml 代码在哪里?
  • 但是在这两种 Java 形式中,我都将 instance/contentView 声明为正确的形式,所以我不确定这会有所作为。但我会试一试。
  • setContentView(R.layout.test2);因此,您需要在两种布局中正确设置按钮名称,然后使用各自的名称调用它们

标签: java android xml navigation


【解决方案1】:

您有两个名为“btnNext”的按钮。这就是它不起作用的原因。
仔细检查:

"@+id/btnNext"

你设置了两次相同的 id...

【讨论】:

    【解决方案2】:

    将 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。然后它会运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多