【问题标题】:Black Screen Error黑屏错误
【发布时间】:2011-08-16 19:39:42
【问题描述】:

所以最近我设置了一个意图,当点击一个按钮时,它会带来一个新的活动。它工作正常,但是当我进入活动然后尝试返回主屏幕时,我得到一个黑屏。在黑屏上,如果我再回击一次,它会将我带到主菜单,但这很烦人。有什么想法吗?

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);

}
}

Manifest 中的 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));
            }

        });
    }
}

【问题讨论】:

    标签: android eclipse android-intent


    【解决方案1】:

    您的第一个活动从不设置布局。在显示的代码中,您似乎要从playbutton 转到PlayActivity。当您按下PlayActivity 时,它会返回到playbutton。由于您从未在playbutton 中的任何布局上调用setContentView(),因此它只是一个没有任何作用的黑屏。看来您应该只删除 playbutton 活动。

    【讨论】:

    • 抱歉,我的措辞可能有误。当我的应用程序启动时,它会启动 DragonFruitActivity.java,它将布局设置为我制作的 xml。然后我使用两个图像按钮作为屏幕上的主要按钮。当按下播放按钮(播放按钮活动)时,我希望它开始一个新活动。这是做事的正确方式吗?我会将 DragonFruitActivity.java 放在我的问题中。
    • 嘿,我明白了,感谢您的帮助,您是对的,播放按钮活动正在采取许多步骤。
    • startActivity(new Intent(DragonFruitActivity.this, playbutton.class)); 替换为startActivity(new Intent(DragonFruitActivity.this, PlayActivity.class)); 没有理由开始新活动来开始新活动。
    • 雅哈哈哈我注意到了。感谢您的帮助!
    【解决方案2】:

    如果您从主线程调用播放按钮 Activity,然后仅在 onCreate() 中调用 PlayActivity,则添加了不必要的步骤。

    动起来

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

    从播放按钮 Activity 到主 Activity

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 1970-01-01
      相关资源
      最近更新 更多