【问题标题】:android: Cannot switch between activities using intentandroid:无法使用意图在活动之间切换
【发布时间】:2013-04-27 21:47:00
【问题描述】:

我很难使用意图从一项活动切换到另一项活动。我的应用程序快完成了,我有几个活动,我可以有意识地在它们之间切换。我在我的项目中添加了一项活动(在 Eclipse 中:File ... New ... AndroidActivity)。正如我习惯的那样,它创建了 .java 和 .xml 文件。在我的主要活动中,我定义了我的按钮 (xml) 和 java 类方法以在 buttonClick 上执行。一切似乎都很好,但是当我点击我的按钮时,什么也没有发生。有什么问题?

这里定义了我的主要活动类“Ponuka”:

package com.example.s_home;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class Ponuka extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    getWindow().setBackgroundDrawableResource(R.drawable.pozadie);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ponuka);
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_ponuka, menu);
    return true;
}

// switch to activity: Osvetlenie  --- works fine
public void obrOsv(View view) {
    Intent intent = new Intent (this, Osvetlenie.class);
    startActivity(intent);  
}

 // switch to activity: Kurenie   --- works fine
 public void obrKur(View view) {
    Intent intent = new Intent (this, Kurenie.class);
    startActivity(intent);
}

 // switch to activity: Ochrana   --- this isnt working
 public void obrBez(View view) {
    Intent intent = new Intent (this, Ochrana.class);
    startActivity(intent);
 System.out.println("ok");           // It will not even print out "ok" 
}

}

这里是activity_ponuka.xml:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="150dp"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lin1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>

<Button
     android:id="@+id/bezpecnost"              <!-- THIS IS THE BUTTON -->
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/zabezp"
      android:onClick="obrBez"

      />

<Button
    android:id="@+id/kuren"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:layout_marginLeft="10dp"
    android:background="@drawable/kurenie"

    android:onClick="obrKur" />

 </LinearLayout>

 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/lin2"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="90dp"
     android:gravity="center"
     android:orientation="horizontal" >

  <Button
    android:id="@+id/osvetl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/osvetlenie"
    android:onClick="obrOsv"

        />

   <Button
    android:id="@+id/pohod"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/pohodlie"
    android:layout_marginLeft="10dp"

     />
     </LinearLayout>
 </RelativeLayout>

还有一点需要注意:创建名为“Ochrana”的新活动时,我必须手动更新 R.java 文件。我在这里做错了什么?

【问题讨论】:

  • 您是否在清单文件中定义了第二个活动?
  • 应该可以,为什么那个按钮没有语法高亮?也许它只是评论。另一种方法 - 在 java 代码中创建对按钮的引用并指定 onclicklistener 但删除“onclick”属性。

标签: android button android-intent android-activity


【解决方案1】:

您不应该在 Android 上使用 System.out.println,而是使用 Log 类记录消息,然后您可以在 LogCat 中找到它。 System.out.println 也可能被重定向到 LogCat,但不能保证。 Why doesn't "System.out.println" work in Android?

您的问题是它启动了与您想要的不同的活动还是根本不启动?因为您正在使用Kurenie.class 创建一个意图,您显然希望在其中启动Ochrana 活动(根据obrBez() 方法上方的评论)。

PS:Zdravim z Brna。 :)

【讨论】:

  • 他不需要调用这个方法,“onclick”属性句柄方法调用
  • 是的,我在发帖后也注意到了这一点。仍然不习惯这种实现方式。谢谢。
  • 扎布里——我的错。在我的源代码中,我正在调用 Ochrana.class 并且它根本没有启动。如果我在其他按钮上添加 system.out.println,它工作正常。所以我想我的问题是那个按钮。但它会是什么?
【解决方案2】:

您永远不应手动更新 R.java,因为如果您的代码正确,它会自动更新。

您还需要在 Activity 中获取按钮的实例并添加 OnClickListener 以调用切换 Activity 的方法。

您需要做的最后一件事是在清单文件中添加您想要使用的活动。

【讨论】:

  • 我知道,它应该自动更新,这就是我想知道的原因。在我的其余代码中,我没有错误,所以我根本不明白我做了什么。现在到你的第二段。我以完全相同的方式制作了其他按钮,它们没有问题。
【解决方案3】:

可能与您的问题无关,但您在新按钮事件中调用了错误的 Activity:

public void obrBez(View view) {
    Intent intent = new Intent (this, Kurenie.class);

【讨论】:

    【解决方案4】:

    我建议将setOnClickListener() 用于按钮,而不是在XML 中使用android:onClick,用于布局和设计的XML ment 以及用于逻辑的Java 活动文件,将两者混合会导致混淆。您也可以尝试解决您的问题(尽管我没有看到您的代码有任何问题):

    Button btn = (Button) findViewById(R.id.bezpecnost);
    btn.setOnClickListener(new OnClickListener() {
        @Override
            public void onClick(View v) {
                 //Assuming you want Ochrana activity from your comment
                 Intent intent = new Intent (this, Ochrana.class); 
                 startActivity(intent);
            }
        });
    

    还有两件事不能解决您的问题,但可能对您有所帮助: 您可能希望为整个活动类定义Intent intent;,而不是在同一变量上创建新意图以节省内存分配。 最后一件事 - 给你的 XML 文件 [Ctrl]+A 而不是 [Ctrl]+i 来自动排序间距 - 它只会有好处(:

    附: 如果您的 R 文件有任何问题 - 只需将其删除! Eclipse 立即自动重新创建它 - 解决各种 R 错误/未更新...

    【讨论】:

    • 我应该删除整个 R.java 吗?
    • 是的。它会询问您是否确定要删除它,不要担心它会立即再次创建。如果没有 - 关闭 Eclipse 并重新打开它 - 欢迎来到 Android ADT 错误的大世界(:
    【解决方案5】:

    答案就在您的 XML 布局中……仔细观察……

    <Button
         android:id="@+id/bezpecnost"              <!-- THIS IS THE BUTTON -->
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/zabezp"
          android:onClick="obrBez"
    
          />
    

    评论应该采用这种格式&lt;!-- .... //--&gt;

    您遗漏了两个正斜杠 - 这就是为什么没有发生任何事情!

    编辑:

    Eclipse 会以绿色而不是 XML 语法高亮颜色突出显示被注释掉的整个块!

    关于R.java——它是在编译时生成的,所以每当你更改你的XML布局时,Android SDK每次都会重新生成R.java文件!

    AndroidManifest.xml 应该在标签application 中包含指定活动的行,如下所示:

    <activity android:name="com.example.s_home.Ochrana" 
              android:label="Ochrana Activity"/>
    

    【讨论】:

    • 我在此处添加此评论只是为了让您知道那个不起作用的特定按钮在哪里。我的源代码中没有这个。
    • 啊,好吧...公平... :)
    • @user217947 - logcat 说什么?
    • 什么都没有。当我单击该按钮时,什么也没有发生,并且 logcat 是“安静的”:)。当我单击其他按钮时,一切似乎都正常,但是该 R 文件并没有为我重新生成。我已经删除了我手动添加的内容。但在这种情况下,项目在我的新活动类中显示了一些错误:setContentView(R.layout.activity_ochrana);
    • logcat 很安静,因为您还没有使用Log.d("...", "..."); 来查看代码流... :)
    【解决方案6】:

    您的xml 完全错误。您的“@+id/pohod”按钮与“@+id/bezpecnost”按钮混合在一起,因为您在第二个线性布局中错过了android:layout_below="@id/lin1"。我测试了下面的布局,没问题。我改为android:text,因为我没有drawable

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_marginTop="150dp"
    >
    
    <LinearLayout 
    android:id="@+id/lin1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    >
    
    <Button
         android:id="@+id/bezpecnost"             
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Ochrana"
          android:onClick="obrBez"
    
          />
    
    <Button
        android:id="@+id/kuren"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_marginLeft="10dp"
        android:text="Kurenie"
    
        android:onClick="obrKur" />
    
     </LinearLayout>
    
     <LinearLayout
    
         android:id="@+id/lin2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="90dp"
         android:gravity="center"
         android:layout_below="@id/lin1"
         android:orientation="horizontal" >
    
      <Button
        android:id="@+id/osvetl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="osvetlenie"
        android:onClick="obrOsv"
    
            />
    
       <Button
        android:id="@+id/pohod"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="pohodlie"
        android:layout_marginLeft="10dp"
    
         />
         </LinearLayout>
     </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 2015-08-25
      • 2011-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多