所以最近我设置了一个意图,当点击一个按钮时会产生一个新的活动。它工作正常,但当我去活动,然后尝试回到主屏幕,我得到一个黑屏。在黑屏幕上如果我再次回击它会把我带到主菜单,但这很烦人。有什么想法吗?
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));
}
});
}
}
答案 0 :(得分:1)
您的第一个活动从未设置布局。在显示的代码中,您似乎从playbutton
转到PlayActivity
。当您向后按PlayActivity
时,它会返回playbutton
。由于您从未在setContentView()
中的任何布局上调用playbutton
,因此它只是一个什么都不做的黑屏。您似乎应该删除playbutton
活动。
答案 1 :(得分:0)
如果从主线程调用playbutton Activity,然后在onCreate()中调用PlayActivity,则会添加一个不必要的步骤。
移动
Intent myIntent = new Intent(playbutton.this, PlayActivity.class);
playbutton.this.startActivity(myIntent);
从播放按钮活动到主要活动