我开发了一个应用程序,当用户按下按钮时播放音乐。我还开发了一个启动画面和应用程序,它确实与应用程序一起工作。但是,我想修改我的应用程序,以便启动画面播放gif文件而不是常规图像。当我通过创建新项目尝试.gif文件时,它确实有效。我用电影课。我尝试了我的启动画面的意图,以便它调用gif.java文件。但它没有奏效。它只是向我展示了我的gif文件的扭曲图像。并且比它说 "你的APP已经崩溃了#34;我的主要java,启动画面和gif .java文件和manifest.xml如下。
Main Java
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(new MYGIFView (this));
setVolumeControlStream(AudioManager.STREAM_MUSIC);
findViewById(R.id.button_1).setOnClickListener(this);
findViewById(R.id.button_2).setOnClickListener(this);
findViewById(R.id.button_3).setOnClickListener(this);
}
public void onClick(View v) {
int resId=1;
// Release any resources from previous MediaPlayer
if (mp != null) {
mp.release();
}
switch (v.getId()) {
case R.id.button_1: resId = R.raw.button_1; break;
case R.id.button_2: resId = R.raw.button_2; break;
case R.id.button_3:
startActivity(new Intent(MainActivity.this,SecondActivity.class));
return;
}
// Create a new MediaPlayer to play this sound
mp = MediaPlayer.create(this, resId);
mp.start();
}
}
启动画面。的java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MYGIFView.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
MYGIF.java
import java.io.InputStream;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Movie;
import android.view.View;
class MYGIFView extends View{
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
public MYGIFView(Context context) {
super(context);
is=context.getResources().openRawResource(R.drawable.th_welcome);
movie=Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;
}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
canvas.scale(3.50f, 3.50f);
movie.draw(canvas,20,20);
this.invalidate();
}
}
的Manifest.xml
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/app_name"/>
</application>
</manifest>
Logcat错误
07-24 17:04:36.332: E/AndroidRuntime(580): FATAL EXCEPTION: main
07-24 17:04:36.332: E/AndroidRuntime(580): android.content.ActivityNotFoundException: Unable to find explicit activity class
{.MYGIFView}; have you declared this activity in your AndroidManifest.xml?
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Activity.startActivityForResult(Activity.java:3190)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.Activity.startActivity(Activity.java:3297)
07-24 17:04:36.332: E/AndroidRuntime(580): at com.$1.run(SplashScreen.java:29)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.os.Handler.handleCallback(Handler.java:605)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.os.Handler.dispatchMessage(Handler.java:92)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.os.Looper.loop(Looper.java:137)
07-24 17:04:36.332: E/AndroidRuntime(580): at android.app.ActivityThread.main(ActivityThread.java:4340)
07-24 17:04:36.332: E/AndroidRuntime(580): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 17:04:36.332: E/AndroidRuntime(580): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 17:04:36.332: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-24 17:04:36.332: E/AndroidRuntime(580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-24 17:04:36.332: E/AndroidRuntime(580): at dalvik.system.NativeStart.main(Native Method)
提前致谢..
答案 0 :(得分:0)
您的代码正在启动一项新的&#34;活动&#34;使用MYGIFView类。 它似乎应该是一种活动,而不是一种观点。
认为只需要
setContentView(new MYGIFView(this));
在SplashActivity的onCreate中,在处理程序声明之前。 并在MainActivity的onCreate中删除它(这似乎是一个错误)。
然后在帖子延迟处理程序中将MyGIFView.class
替换为MainActivity.class
:
Intent i = new Intent(SplashScreen.this, MainActivity.class);
还需要删除主要活动清单的LAUNCHER
和MAIN
参数:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
已修改:erata SecondActivity - &gt; MainActivity