添加此行后,按钮点击时应用程序崩溃,此应用程序有3种模式"简单,中等和硬"。在我添加此行之前(下面的下一个第二个代码旁边)我可以在开始时选择a模式后再选择另一个模式然后返回。但添加此行后,当我第二次选择游戏模式时,应用程序崩溃(首先选择没问题)
StartAppSDK.init(this, "*MY APP ID", true);
旁边的
super.onCreate(savedInstanceState);
完整活动代码
package com.myapps.app;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import com.myapps.app.diamen.Dimension;
import com.startapp.android.publish.StartAppSDK;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ViewSwitcher;
public class PuzzleActivity extends Activity {
public static final int DIALOG_PAUSED_ID = 44;
GameBoard board;
int screenOrientation;
Bitmap sourceImage;
Button preview, back;
ViewSwitcher inGameViewSwitcher;
Typeface custom_font;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StartAppSDK.init(this, "209057502", true);
custom_font = Typeface.createFromAsset(getAssets(), "font.otf");
/*preview = (Button)findViewById(R.id.previewButton);
preview.setTypeface(custom_font);
back = (Button)findViewById(R.id.backToGameButton);
back.setTypeface(custom_font);*/
// Prepare the Interstitial Ad
screenOrientation = getIntent().getIntExtra(MainMenuActivity.EXTRA_BOARD_ORIENTATION, 1);
// String str = screenOrientation == 0 ? "PORTRAIT" : "HORIZONTAL";
// Log.d("KAMIL", "Orientation recorded by puzzleactivity: " + str);
//locking the app in needed position
if(screenOrientation == GameBoard.ORIENTATION_PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//making the app full screen
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.puzzle);
//Setting backgrounds to black.
((RelativeLayout) findViewById(R.id.centerLayout)).setBackgroundColor(Color.BLACK);
((FrameLayout) findViewById(R.id.backgroundLayout)).setBackgroundColor(Color.BLACK);
inGameViewSwitcher = (ViewSwitcher) findViewById(R.id.inGameViewSwitcher);
//Crating a game board.
board = new GameBoard(decodeGameSizeFromIntent(),
(RelativeLayout) findViewById(R.id.centerLayout),
screenOrientation, this);
sourceImage = loadBitmapFromIntent();
ImageView preview = (ImageView) findViewById(R.id.previewImageView);
preview.setImageBitmap(sourceImage);
PuzzleCreator creator = new PuzzleCreator(sourceImage, board);
board.loadTiles(creator.createPuzzle());
board.drawBoard();
}
private Dimension decodeGameSizeFromIntent(){
Dimension size = null;
String str = getIntent().getStringExtra(MainMenuActivity.EXTRA_GAMESIZE);
String[] gameSizes = getResources().getStringArray(R.array.gamesizes);
if(str.equals(gameSizes[0])) size = new Dimension(3,5);
else if(str.equals(gameSizes[1])) size = new Dimension(4,7);
else if(str.equals(gameSizes[2])) size = new Dimension(6,10);
else
throw new RuntimeException("Decoding game size from intent failed. String does not match.");
return size;
}
private Bitmap loadBitmapFromIntent(){
Bitmap selectedImage = null;
/*try{
InputStream imageStream = getContentResolver().openInputStream(imgUri);
selectedImage = BitmapFactory.decodeStream(imageStream);
}catch(FileNotFoundException ex){
Log.e("LOADING ERROR", "Cannot load picture from the URI given", ex);
}*/
try
{
Uri imgUri = (Uri) getIntent().getParcelableExtra(MainMenuActivity.EXTRA_IMGURI);
//Opening the input stream and receiving Bitmap.
InputStream imageStream = getContentResolver().openInputStream(imgUri);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 2;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
selectedImage = BitmapFactory.decodeStream(imageStream,null,options);
if (selectedImage.getHeight() > 1200)
selectedImage = Bitmap.createScaledBitmap(selectedImage, selectedImage.getWidth() *1200 / selectedImage.getHeight(), 1200, false);
System.gc();
//return selectedImage;
}
catch(FileNotFoundException ex){
Log.e("LOADING ERROR", "Cannot load picture from the URI given", ex);
}
/*
if(selectedImage.getWidth()>selectedImage.getHeight()){
selectedImage = BitmapOperator.rotateBitmap(selectedImage, 90);
}*/
return selectedImage;
}
@Override
protected void onRestart() {
super.onRestart();
//Intent intent = new Intent(this, MainMenuActivity.class);
//startActivity(intent);
//showDialog(DIALOG_PAUSED_ID);
}
public Bitmap getPreview(String fileName) {
File image = new File(fileName);
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1)) {
return null;
}
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / 64;
// opts.inSampleSize = originalSize;
return BitmapFactory.decodeFile(image.getPath());
}
@SuppressWarnings("deprecation")
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
int reqHeight) { // BEST QUALITY MATCH
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float) height / (float) reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth) {
// if(Math.round((float)width / (float)reqWidth) > inSampleSize) //
// If bigger SampSize..
inSampleSize = Math.round((float) width / (float) reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
@Override
public void onBackPressed() {
//showDialog(DIALOG_PAUSED_ID);
super.onBackPressed();
}
public void inGameButtonsOnClick(View view){
switch(view.getId()){
case R.id.previewButton:
inGameViewSwitcher.showNext();
break;
case R.id.backToGameButton:
inGameViewSwitcher.showPrevious();
break;
}
}
@Override
public void onResume() {
System.gc();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
System.gc();
}
protected void onDestroy()
{
super.onDestroy();
System.gc();
}
}
请帮助:(