因此,在我的主要活动中,我添加了一个图像按钮,它工作正常。当我添加另一个时,我开始强制关闭。我将其链接到新活动并将新活动放入清单文件中。如果我删除第二个图像按钮的代码并运行应用程序,则可以正常工作。我的启动画面实际上是我的主要活动,但我认为这不是问题。预先感谢您可以给我的任何帮助。
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)
最佳答案
01-01 18:25:59.553:E / global(11497):不支持不赞成使用的线程方法。
这几乎说明了整个故事。查看Thread class documentation并查看不赞成使用的方法。检查您是否正在使用其中的一个或多个,并将其从代码中移除¹。该文档还说明了不应使用这些方法的原因。
¹例如您正在使用不推荐使用的方法Thread.stop()