Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当屏幕关闭时,应用程序停止工作_Android_Android Resources - Fatal编程技术网

Android 当屏幕关闭时,应用程序停止工作

Android 当屏幕关闭时,应用程序停止工作,android,android-resources,Android,Android Resources,我已经创建了一个android应用程序,它在横向定向模式下工作。当屏幕处于活动状态时,它会使屏幕保持打开状态。但是,当我使用电源按钮锁定屏幕且应用程序打开时,它停止工作,并在LogCat中给出Resources$NotFoundException,指向活动的onCreate()方法。请帮忙 MenuActivity.java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(save

我已经创建了一个android应用程序,它在
横向
定向模式下工作。当屏幕处于活动状态时,它会使屏幕保持打开状态。但是,当我使用电源按钮锁定屏幕且应用程序打开时,它停止工作,并在
LogCat
中给出
Resources$NotFoundException
,指向活动的
onCreate()
方法。请帮忙

MenuActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_menu); // Error points here
    bjmc.activity = this;
    preference = getSharedPreferences("BJLocalPlayer", Context.MODE_PRIVATE);
    if (!preference.contains("balance") || (preference.getLong("balance", 0)) < 100) {
        Editor edit = preference.edit();
        edit.putLong("balance", 1000);
        edit.commit();
    }
    ((TextView) findViewById(R.id.player_balance)).setText(Long
            .toString(preference.getLong("balance", 0)));

    tvAmerican = new TextView(this);
    tvAmerican.setGravity(Gravity.CENTER);
    tvAmerican.setBackgroundColor(0x40000000);
    tvAmerican.setTextColor(getResources().getColor(R.color.orangish_yellow));
    tvAmerican.setText("American");
    tvEuropean = new TextView(this);
    tvEuropean.setGravity(Gravity.CENTER);
    tvEuropean.setText("European");

    final TabHost th = (TabHost) findViewById(R.id.table_tab_host);
    th.setup();
    th.addTab(th.newTabSpec("American").setIndicator(tvAmerican)
            .setContent(R.id.american));
    th.addTab(th.newTabSpec("European").setIndicator(tvEuropean)
            .setContent(R.id.european));
    th.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
            switch (tabId) {
            case "American":
                tvAmerican.setBackgroundColor(0x40000000);
                tvAmerican.setTextColor(getResources().getColor(R.color.orangish_yellow));
                tvEuropean.setBackgroundColor(0x00000000);
                tvEuropean.setTextColor(getResources().getColor(R.color.total_text_color));
                break;
            case "European":
                tvAmerican.setBackgroundColor(0x00000000);
                tvAmerican.setTextColor(getResources().getColor(R.color.total_text_color));
                tvEuropean.setBackgroundColor(0x40000000);
                tvEuropean.setTextColor(getResources().getColor(R.color.orangish_yellow));
                break;
            }
        }
    });
    final ViewPager american = (ViewPager) findViewById(R.id.american);
    american.setAdapter(new ImagePagerAdapter(getLayoutInflater(),
            new int[] { R.drawable.am_table0, R.drawable.am_table1,
                    R.drawable.am_table2, R.drawable.am_table3,
                    R.drawable.am_table4 }));
    final ViewPager european = (ViewPager) findViewById(R.id.european);
    european.setAdapter(new ImagePagerAdapter(getLayoutInflater(),
            new int[] { R.drawable.eu_table0, R.drawable.eu_table1,
                    R.drawable.eu_table2, R.drawable.eu_table3,
                    R.drawable.eu_table4 }));

    ((Button) findViewById(R.id.button_play_single_player)).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    tableIndex = (th.getCurrentTab() == 0 ? american.getCurrentItem()
                            : european.getCurrentItem() + 5);
                    bjmc.startGameForTableIndex(tableIndex,
                        BJMatchCoordinator.SINGLEPLAYER, new BJMCDelegate(MenuActivity.this));
                }
            });

    ((Button) findViewById(R.id.button_play_bluetooth)).setOnClickListener(new BluetoothListener());
}
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iapp.bjp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.iapp.bjp.MenuActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="sensorLandscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.iapp.bjp.SinglePlayerAddBetActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_single_player_add_bet"
            android:parentActivityName="com.iapp.bjp.MenuActivity"
            android:screenOrientation="sensorLandscape" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.iapp.bjp.MenuActivity" />
        </activity>
        <activity
            android:name="com.iapp.bjp.SinglePlayerGameActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_single_player_game"
            android:screenOrientation="sensorLandscape" >
        </activity>
        <activity
            android:name="com.iapp.bjp.bluetooth.ScanDeviceActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_scan_device" >
        </activity>
    </application>

</manifest>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menu_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_menu"
    android:gravity="center"
    android:keepScreenOn="true"
    android:orientation="horizontal"
    android:baselineAligned="false"
    tools:context="com.iapp.bjp.MenuActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/player_balance_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_balance_text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/player_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <TabHost
        android:id="@+id/table_tab_host"
        android:layout_width="0dip"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_weight="2" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                style="@android:style/Widget.Holo.TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <android.support.v4.view.ViewPager
                    android:id="@+id/american"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </android.support.v4.view.ViewPager>

                <android.support.v4.view.ViewPager
                    android:id="@+id/european"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </android.support.v4.view.ViewPager>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button_play_single_player"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_btn_ph"
            android:text="@string/single_play_button_text" />

        <Button
            android:id="@+id/button_play_bluetooth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_btn_ph"
            android:text="@string/bluetooth_button_text" />
    </LinearLayout>

</LinearLayout>

更新2

活动菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iapp.bjp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.iapp.bjp.MenuActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:screenOrientation="sensorLandscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.iapp.bjp.SinglePlayerAddBetActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_single_player_add_bet"
            android:parentActivityName="com.iapp.bjp.MenuActivity"
            android:screenOrientation="sensorLandscape" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.iapp.bjp.MenuActivity" />
        </activity>
        <activity
            android:name="com.iapp.bjp.SinglePlayerGameActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_single_player_game"
            android:screenOrientation="sensorLandscape" >
        </activity>
        <activity
            android:name="com.iapp.bjp.bluetooth.ScanDeviceActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/title_activity_scan_device" >
        </activity>
    </application>

</manifest>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menu_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_menu"
    android:gravity="center"
    android:keepScreenOn="true"
    android:orientation="horizontal"
    android:baselineAligned="false"
    tools:context="com.iapp.bjp.MenuActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/player_balance_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/player_balance_text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/player_balance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

    <TabHost
        android:id="@+id/table_tab_host"
        android:layout_width="0dip"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_weight="2" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                style="@android:style/Widget.Holo.TextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <android.support.v4.view.ViewPager
                    android:id="@+id/american"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </android.support.v4.view.ViewPager>

                <android.support.v4.view.ViewPager
                    android:id="@+id/european"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >
                </android.support.v4.view.ViewPager>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button_play_single_player"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_btn_ph"
            android:text="@string/single_play_button_text" />

        <Button
            android:id="@+id/button_play_bluetooth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/home_btn_ph"
            android:text="@string/bluetooth_button_text" />
    </LinearLayout>

</LinearLayout>


注意:请记住,该应用程序在正常使用情况下工作正常。问题发生在我使用电源按钮关闭屏幕时。

从清单中删除“android:screenOrientation=“Sensor横向”,并发布活动菜单。xml

问题是我将布局xml文件保存在
布局栏
文件夹中,而不是
布局
文件夹中。当我锁定屏幕时,它在内部转为纵向模式(不知道为什么它总是保持该方向),并且由于它无法找到纵向模式的资源,它给出了
resources$NotFoundException

post logcat+您的清单file@MikeM. 试过了。没有区别:(@shayan It给出错误“您的tabhost必须有一个id为android.R.id.tabs的tabwidget”和“您的tabhost必须有一个id为android.R.id.tabcontent的framelayout”@MikeM.尝试了您的建议。没有区别。@MikeM。我的布局在layout land文件夹中,这就是它找不到它们的原因。谢谢您的时间:)我添加了
活动\u菜单
。如果我删除
sensorscape
,我如何将应用程序保持在
scape
方向?我的资源位于layout land文件夹中,这就是它找不到它们的原因。感谢您抽出时间:)