2011-08-16 110 views
0

所以最近我設置了一個意圖,當點擊一個按鈕時,它會引發一個新的活動。它工作正常,但當我去活動,然後嘗試返回到主屏幕,我得到一個黑屏。在黑屏上,如果我再回來一次,它會將我帶到主菜單,但這很煩人。有任何想法嗎?黑屏錯誤

playbutton.java

package com.Dragon_Fruit; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 

public class playbutton extends Activity { 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     super.onCreate(savedInstanceState); 
     Intent myIntent = new Intent(playbutton.this, PlayActivity.class); 
     playbutton.this.startActivity(myIntent); 

    } 

} 

PlayActivity.java

package com.Dragon_Fruit; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager; 

public class PlayActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.playscreen); 

} 
} 

PlayActivity在清單

<activity android:name=".PlayActivity" 
        android:label="@string/app_name" 
      android:screenOrientation="landscape" 
        android:configChanges="keyboard|keyboardHidden|orientation"> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

DragonFruitActivity.java

package com.Dragon_Fruit; 

import java.io.IOException; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ImageButton; 

public class DragonFruitActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // ***BUTTON SOUND***// 
     final MediaPlayer buttonSound = MediaPlayer.create(
       DragonFruitActivity.this, R.raw.button_click); 

     ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton); 
     playbutton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       arg0.setBackgroundResource(R.drawable.playbuttonselected); 
       // TODO Auto-generated method stub 
       if(buttonSound.isPlaying()) { 
        buttonSound.stop(); 
       } 

       try { 
        buttonSound.prepare(); 
       } catch (IllegalStateException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       buttonSound.start(); 

       startActivity(new Intent(DragonFruitActivity.this, 
         playbutton.class)); 
      } 

     }); 
     ImageButton settingsbutton = (ImageButton) findViewById(R.id.settingsbutton); 
     settingsbutton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       if(buttonSound.isPlaying()) { 
        buttonSound.stop(); 
       } 

       try { 
        buttonSound.prepare(); 
       } catch (IllegalStateException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       buttonSound.start(); 

       startActivity(new Intent(DragonFruitActivity.this, 
         settingsbutton.class)); 
      } 

     }); 
    } 
} 

回答

1

你的第一個活動是永遠不落的佈局。在顯示的代碼中,它看起來像從playbuttonPlayActivity。當您按下PlayActivity時,它會回到playbutton。由於您從未在playbutton的任何佈局上調用過setContentView(),這只是一個什麼都不做的黑屏。看起來你應該刪除playbutton活動。

+0

對不起,我可能會措辭是錯誤的。當我的應用程序啓動時,它會啓動DragonFruitActivity.java,它將佈局設置爲我製作的xml。然後我使用了兩個圖像按鈕作爲屏幕上的主要按鈕。當播放按鈕(播放按鈕活動)被推動時,我希望它開始一個新的活動。這是關於事情的正確方法嗎?我將把DragonFruitActivity.java放在我的問題中。 – Zach

+0

嘿,我想通了,謝謝你的幫助,你是正確的playbutton活動採取了很多步驟。 – Zach

+0

用'startActivity(new Intent(DragonFruitActivity.this,PlayActivity.class))替換'startActivity(new Intent(DragonFruitActivity.this,playbutton.class));''沒有理由開始一個新的活動來開始一個新的活動。 – DeeV

0

如果您從主線程調用playbutton Activity,然後在onCreate()中調用PlayActivity,則會添加不必要的步驟。

只是移動

Intent myIntent = new Intent(playbutton.this, PlayActivity.class); 
    playbutton.this.startActivity(myIntent); 

從你PLAYBUTTON活動,以您的主要活動

+0

哦,這很有道理謝謝它現在工作正常。 – Zach