我能够加载启动画面,但是当MainActivty加载应用程序崩溃时,我想在启动画面之后加载Main Activity。此外,我发现前面的堆栈溢出讨论了相同的主题,但是没有帮了我。
初始屏幕的行为符合预期,但应用程序在显示主要活动之前崩溃。主要活动是带有图像适配器的网格视图,该适配器将选定的项目从网格视图放大到屏幕底部的图像视图组件。
这是课堂的最后一个项目。
SplashActivity
package net.androidbootcamp.catchofthedayfishingapp;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;
public class SplashActivity extends AppCompatActivity {
MediaPlayer mpReel;
int playing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
mpReel = new MediaPlayer();
mpReel = MediaPlayer.create(this, R.raw.reel);
playing = 0;
mpReel.start();
TimerTask task = new TimerTask() {
@Override
public void run() {
mpReel.stop();
finish();
startActivity(new Intent(SplashActivity.this, MainActivity.class));
} // end run
}; // end new TimerTask
// schedule splash screen to be displayed for 5 seconds (= 5000 ms)
Timer opening = new Timer();
opening.schedule(task, 5000);
} // end onCreate
}
MainActivty
package net.androidbootcamp.catchofthedayfishingapp;
import android.content.Context;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Integer[] Animals = {R.drawable.bluefin, R.drawable.yellowtail, R.drawable.barracuda,
R.drawable.seabass, R.drawable.bass, R.drawable.rainbow,};
ImageView pic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
GridView grid = (GridView)findViewById(R.id.gridView);
final ImageView pic = (ImageView)findViewById(R.id.imgLarge);
grid.setAdapter(new ImageAdapter(this));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), "Selected Species " + (position+1), Toast.LENGTH_SHORT).show();
pic.setImageResource(Animals[position]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter (Context c) {
context = c;
}
@Override
public int getCount() {
return Animals.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
pic = new ImageView(context);
pic.setImageResource(Animals[position]);
pic.setScaleType(ImageView.ScaleType.FIT_XY);
pic.setLayoutParams(new GridView.LayoutParams(300,270));
return pic;
}
} // end class ImageAdapter
}