簡體   English   中英

android:無法使用意圖在活動之間切換

[英]android: Cannot switch between activities using intent

我很難用意圖從一種活動切換到另一種活動。 我的應用程序快完成了,我有幾個活動,我可以有意地在它們之間切換。 我在項目中又添加了一個活動(在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文件。 我在這里做錯了什么?

您不應該在Android上使用System.out.println ,而是使用Log類記錄消息,然后在LogCat中找到它。 System.out.println也可能會重定向到LogCat,但不能保證。 為什么“ System.out.println”在Android中不起作用?

您的問題是它啟動的活動與您想要的活動不同,還是根本沒有啟動? 因為您正在用Kurenie.class創建一個意圖,您顯然要在其中啟動Ochrana活動(根據obrBez()方法上方的obrBez() )。

附言:Zdravim z Brna。 :)

永遠不要手動更新R.java,因為如果您的代碼正確,它會自動更新。

您還需要在活動中獲取按鈕的實例,並添加OnClickListener來調用切換活動的方法。

最后要做的就是在清單文件中添加要使用的那些活動。

可能與您的問題無關,但您在新按鈕事件中調用了錯誤的活動:

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

我建議對按鈕使用setOnClickListener()而不是在XML中使用android:onClick在布局和設計中使用XML-XML,在邏輯上使用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自動排序間距-這只會做的很好(:

PS:如果您的R文件有任何問題,請刪除它! Eclipse自動立即重新創建它-解決了各種R不良/未更新的情況...

答案就在您的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"

      />

注釋應采用以下格式: <!-- .... //-->

您省略了兩個正斜杠-這就是為什么什么都沒發生的原因!

編輯:

Eclipse會顯示注釋掉的整個塊以綠色突出顯示,而不是XML語法突出顯示顏色!

關於R.java ,它是在編譯時生成的,因此,每當您更改XML布局時,Android SDK都會每次重新生成R.java文件!

AndroidManifest.xml應該具有在標簽application指定活動的行,如下所示:

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

您的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM