由于图像按钮强制关闭

时间:2012-01-01 23:16:20

标签: java android eclipse android-layout android-button

所以在我的主要活动中我添加了一个图像按钮,它工作正常。当我去添加另一个时,我开始强制关闭。我将它链接到一个新活动,并将新活动放在清单文件中。如果我删除第二个图像按钮的代码并运行应用程序它工作正常。我的启动画面实际上是我的主要活动,但我不认为这是问题所在。提前感谢您提供的任何帮助。

MainActivity(新):

   package com.crazycastles;


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



public class MainActivity extends Activity implements OnClickListener {
    /** 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);

        //CREATE BUTTON 1 & SOUND
        final MediaPlayer buttonSound = MediaPlayer.create(
                MainActivity.this, R.raw.swords);

        ImageButton button1 = (ImageButton) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        button1Activity.class));
            }
        }); 
        ImageButton multiplayerbutton = (ImageButton) findViewById(R.id.multiplayerbutton);
        multiplayerbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                buttonSound.start();
                startActivity(new Intent(MainActivity.this,
                        multiplayerbuttonActivity.class));
            }
        }); 

        //END OF BUTTON1 & SOUND



        }
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    }

清单文件:

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

    <uses-sdk android:minSdkVersion="3"></uses-sdk>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:debuggable="true">


        <activity
            android:name=".SplashScreen"
            android:label="Crazy Castles" 
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="landscape"
            android:configChanges="keyboard|keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="MainActivity" 
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
            android:launchMode="standard" 
            android:permission="android.permission.WAKE_LOCK"
            android:configChanges="keyboard|keyboardHidden|orientation">
        </activity>
        <activity android:name="button1Activity" 
           android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
            android:launchMode="standard" 
            android:permission="android.permission.WAKE_LOCK"
            android:configChanges="keyboard|keyboardHidden|orientation"
            android:screenOrientation="portrait"></activity>

        <activity android:name="multiplayerbuttonActivity" 
           android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
            android:launchMode="standard" 
            android:permission="android.permission.WAKE_LOCK"
            android:configChanges="keyboard|keyboardHidden|orientation"
            android:screenOrientation="portrait"></activity>



        </application>

</manifest>

闪屏:

package com.crazycastles;

import java.io.IOException;

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

public class SplashScreen extends Activity {

    /**
     * The thread to process splash screen events
     */
    private Thread mSplashThread;    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Splash screen view
        setContentView(R.layout.splash);
        final MediaPlayer buttonSound = MediaPlayer.create(
                SplashScreen.this, R.raw.swords);

        final SplashScreen sPlashScreen = this;   

        // The thread to wait for splash screen events
        mSplashThread =  new Thread(){
            @Override
            public void run(){
                try {
                    synchronized(this){
                        // Wait given period of time or exit on touch
                        wait(3000);
                        buttonSound.prepare();
                    }
                }
                catch(InterruptedException ex){                    
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finish();

                // Run next activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, MainActivity.class);
                startActivity(intent);
                stop();                    
            }
        };

        mSplashThread.start();        
    }

    /**
     * Processes splash screen touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent evt)
    {
        if(evt.getAction() == MotionEvent.ACTION_DOWN)
        {
            synchronized(mSplashThread){
                mSplashThread.notifyAll();
            }
        }
        return true;
    }    
} 

新版LogCat:

    01-01 19:51:35.068: E/MediaPlayer(12462): prepareAsync called in state 8
01-01 19:51:35.427: E/AndroidRuntime(12462): FATAL EXCEPTION: main
01-01 19:51:35.427: E/AndroidRuntime(12462): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.crazycastles/com.crazycastles.MainActivity}: java.lang.ClassCastException: android.widget.ImageView
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.app.ActivityThread.access$2500(ActivityThread.java:129)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.os.Looper.loop(Looper.java:143)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.app.ActivityThread.main(ActivityThread.java:4701)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at java.lang.reflect.Method.invokeNative(Native Method)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at java.lang.reflect.Method.invoke(Method.java:521)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at dalvik.system.NativeStart.main(Native Method)
01-01 19:51:35.427: E/AndroidRuntime(12462): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-01 19:51:35.427: E/AndroidRuntime(12462):    at com.crazycastles.MainActivity.onCreate(MainActivity.java:41)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-01 19:51:35.427: E/AndroidRuntime(12462):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)

1 个答案:

答案 0 :(得分:3)

  

01-01 18:25:59.553:E / global(11497):不支持弃用的Thread方法。

这几乎讲述了整个故事。查看Thread class documentation并查看已弃用的方法。检查您是否使用其中一个或多个并从代码中删除它们。该文档还说明了为什么不应该使用这些方法的原因。

¹例如。您使用的是已弃用的方法Thread.stop()