所以最近我設置了一個意圖,當點擊一個按鈕時,它會引發一個新的活動。它工作正常,但當我去活動,然後嘗試返回到主屏幕,我得到一個黑屏。在黑屏上,如果我再回來一次,它會將我帶到主菜單,但這很煩人。有任何想法嗎?黑屏錯誤
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));
}
});
}
}
對不起,我可能會措辭是錯誤的。當我的應用程序啓動時,它會啓動DragonFruitActivity.java,它將佈局設置爲我製作的xml。然後我使用了兩個圖像按鈕作爲屏幕上的主要按鈕。當播放按鈕(播放按鈕活動)被推動時,我希望它開始一個新的活動。這是關於事情的正確方法嗎?我將把DragonFruitActivity.java放在我的問題中。 – Zach
嘿,我想通了,謝謝你的幫助,你是正確的playbutton活動採取了很多步驟。 – Zach
用'startActivity(new Intent(DragonFruitActivity.this,PlayActivity.class))替換'startActivity(new Intent(DragonFruitActivity.this,playbutton.class));''沒有理由開始一個新的活動來開始一個新的活動。 – DeeV