2014-02-17 48 views
0

以下是源代碼。 這個程序只是做內存匹配遊戲,並在android 4.4 package com.example.myimagematchgamev10;我的應用程序在Android 4.4上運行良好,但在2.3上崩潰。甚至沒有啓動

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.graphics.drawable.Drawable; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.os.Build; 
import android.widget.Button; 
import android.widget.ImageButton; 

import java.util.HashMap; 
import java.util.Random; 

class MyCustomImageButton extends ImageButton 
{ 
    protected int holdingImage; 

    public MyCustomImageButton(Context context) { 
     super(context); 
     this.holdingImage = -1; 
    } 
    public MyCustomImageButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.holdingImage = -1; 
    } 
    public MyCustomImageButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     this.holdingImage = -1; 
    } 
} 
//public class MainActivity extends ActionBarActivity { 
    public class MainActivity extends Activity { 
    Random myR = new Random(); 
    MyCustomImageButton[] myImageButtons; 
    int[] myImages={R.drawable.one,R.drawable.two,R.drawable.five,R.drawable.seven,R.drawable.eight,R.drawable.six,R.drawable.three,R.drawable.four}; 
    int[] myImageButtonArray = {R.id.imageButton0, R.id.imageButton1, R.id.imageButton2, R.id.imageButton3, R.id.imageButton4, 
      R.id.imageButton5, R.id.imageButton6, R.id.imageButton7, R.id.imageButton8, R.id.imageButton9, 
      R.id.imageButton10, R.id.imageButton11, R.id.imageButton12, R.id.imageButton13, R.id.imageButton14, 
      R.id.imageButton15}; 

    // The following four lies for holding temp data while checking the match 
    int myFirstButtonImage = R.drawable.ic_launcher; 
    int mySecondButtonImage = R.drawable.ic_launcher; 
    ImageButton myFirstButton = null; 
    ImageButton mySecondButton = null; 

    // for message box 
    int myNoOfClick = 0; 
    int myNoOfCompletedCells = 0; 

    Button myNewgame; 
    AlertDialog.Builder alertDialog; // = new AlertDialog.Builder(MainActivity.this); 

    protected void initializeGrid() 
    { 
     for (int i=0; i < 16; ++i) 
     { 
      myImageButtons[i].setBackgroundResource(R.drawable.ic_launcher); 
      myImageButtons[i].setClickable(true); 
      myImageButtons[i].holdingImage = -1; 
     } 
     myNoOfClick = 0; 
     myNoOfCompletedCells = 0; 
    } 

    protected void createGrid() 
    { 
     myImageButtons = new MyCustomImageButton[16]; 
     for (int i=0; i < 16; ++i) 
     { 
      myImageButtons[i] = (MyCustomImageButton) findViewById(myImageButtonArray[i]); 

     } 
    } 

    protected void loadImages() 
    { 
     int myNextInt; 
     for (int i=0; i < 8; ++i) 
     { 
      for(int j=0; j < 2; ++j) 
      { 
       myNextInt = myR.nextInt(16); 
       while (myImageButtons[myNextInt].holdingImage != -1) 
       { 
        myNextInt = myR.nextInt(16); 
       } 
       if(myImageButtons[myNextInt].holdingImage == -1) 
       { 
        myImageButtons[myNextInt].setBackgroundResource(R.drawable.ic_launcher); 
        myImageButtons[myNextInt].holdingImage = myImages[i]; 
        //myGridValues[myNextInt] = i+1; 
        // myMap.put(myImageButtonArray[myNextInt],myImages[i]); 
       } 
      } 

     } 
    } 


    protected void showMessage() 
    { 
     alertDialog = new AlertDialog.Builder(MainActivity.this); 
     //alertDialog = new AlertDialog.Builder(MainActivity.this).create(); 
     alertDialog.setTitle("Score"); 
     alertDialog.setMessage("No. Of clicks are: " + myNoOfClick +" !"); 
     alertDialog.setInverseBackgroundForced(true); 
     alertDialog.setCancelable(true); 
     alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){ 

      public void onClick(DialogInterface dialog, 
       int which) { 
        dialog.dismiss(); 
       } 
     }); 
     alertDialog.show(); 
    } 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     createGrid(); 
     initializeGrid(); 
     loadImages();; 

     myNewgame = (Button) findViewById(R.id.newGame); 
     myNewgame.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       initializeGrid(); 
       loadImages(); 
      } 
     }); 


     for(int i=0; i<myImageButtons.length; ++i) 
     { 
      final MyCustomImageButton myTempImageButton = myImageButtons[i]; 
      //final int myIBId = myTempImageButton.getId(); 
      myTempImageButton.setOnClickListener(new View.OnClickListener() { 
       //@Override 
       public void onClick(View view) { 
        ++myNoOfClick; 
        myTempImageButton.setBackgroundResource(myTempImageButton.holdingImage); 
        if (myFirstButtonImage == R.drawable.ic_launcher) 
        { 
         if(mySecondButtonImage == R.drawable.ic_launcher) 
         { 
          myFirstButtonImage = myTempImageButton.holdingImage; 
          myFirstButton = myTempImageButton; 
          myTempImageButton.setClickable(false); 
          myNoOfCompletedCells += 2; 
          if (myNoOfCompletedCells >= 16) 
          { 
           showMessage(); 
          } 
         } 
         else 
         { 
          myFirstButton.setClickable(true); 
          mySecondButton.setClickable(true); 
          myFirstButton.setBackgroundResource(R.drawable.ic_launcher); 
          mySecondButton.setBackgroundResource(R.drawable.ic_launcher); 

          myFirstButton = myTempImageButton; 
          myFirstButtonImage = myTempImageButton.holdingImage; 
          myTempImageButton.setClickable(false); 
         } 
        } 
        else 
        { 
         myTempImageButton.setClickable(false); 
         if (myFirstButtonImage == myTempImageButton.holdingImage) // For Success full Match 
         { 
          myFirstButton.setClickable(false); 
          myFirstButtonImage = R.drawable.ic_launcher; 
          mySecondButtonImage = R.drawable.ic_launcher; 
         } 
         else 
         { 
          mySecondButton = myTempImageButton; 
          mySecondButtonImage = myTempImageButton.holdingImage; 
          myFirstButtonImage = R.drawable.ic_launcher; 
         } 
        } 
       } 
      }); 
     } 

    } 


} 




The above is the source file 

我在清單XML以下

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

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="10" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.myimagematchgamev10.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> 
    </application> 

</manifest> 

上面一個清單XML

下面是我得到

等待設備的日誌。 「C:\ Program Files文件(x86)的\ Android的\ Android的工作室\ SDK \ TOOLS \ emulator.exe」 -avd AVD_for_Nexus_S -netspeed全-netdelay沒有

設備連接:模擬器-5554 設備在線:模擬器-5554 目標設備:AVD_for_Nexus_S [模擬器-5554] 上傳文件 本地路徑:d:\的Android \ MyApplicationProject \ MyImageMatchgameV1.0Project \ MyImageMatchgameV1.0 \建立\ APK \ MyImageMatchgameV1.0-調試unaligned.apk 遠程路徑:/data/local/tmp/com.example.myimagematchgamev10 正在安裝com.example.myimagematchgamev10 DEVICE SHELL命令:pm install -r「/data/local/tmp/com.example.myimagematchgamev10」 pkg:/ data/local/tmp/com.example.myimagematchgamev10 成功

啓動應用程序:com.example.myimagematchgamev10/com.example.myimagematchgamev10.MainActivity。 DEVICE SHELL COMMAND:am start -n「com.example.myimagematchgamev10/com.example.myimagematchgamev10.MainActivity」-a android.intent.action.MAIN -c android.intent.category.LAUNCHER Starting:Intent {act = android .intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example.myimagematchgamev10/.MainActivity}

+5

簡單。 LogcatNotFoundException。 – Enrichman

+0

@Enrichman哈哈不錯。 –

+0

@Enrichman也可能是LogcatNotPostedException ;-) – donfuxx

回答

2

從我可以告訴沒有日誌,你正在使用一個API薑餅。

剛剛閱讀LogCat,它應該給你提示關於不起作用的確切功能。

+0

您能否讓我知道在哪裏看日誌?我對Android的發展完全陌生,並且新增了編輯器。我正在使用Android工作室。 – Sankarlal

+0

啓動應用程序:com.example.myimagematchgamev10/com.example.myimagematchgamev10.MainActivity。 DEVICE SHELL COMMAND:am start -n「com.example.myimagematchgamev10/com.example.myimagematchgamev10.MainActivity」-a android.intent.action.MAIN -c android.intent.category.LAUNCHER Starting:Intent {act = android .intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example.myimagematchgamev10/.MainActivity} [1] Segmentation fault am start -n「com ... – Sankarlal

相關問題