下面是源代码。这个程序只是做内存匹配游戏,在Android4.4包com.example.myimagematchgamev10中运行良好;
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-dios\sdk\tools\emulator.exe"-avdAVD_for_Nexus_S-netSpeed ful-net延迟无
设备已连接:仿真器-5554设备在线:仿真器-5554目标设备:AVD_for_Nexus_S[仿真器-5554]上传文件本地路径:D:\Android\MyApplication ationProject\MyImageMatchgameV1.0Project\MyImageMatchgameV1.0\build\apk\MyImageMatchgameV1.0-debug-unaligned.apk远程路径: /data/local/tmp/com.example.myimagematchgamev10安装com.example.myimagematchgamev10 DEVICE SHELL COMMAND: pm install-r"/data/local/tmp/com.example.myimagematchgamev10"pkg: /data/local/tmp/com.example.myimagematchgamev10成功
正在启动应用程序:com。实例myimagematchgamev10/com。实例myimagematchgamev10。主要活动。设备SHELL命令:am start-n“com.example.myimagematchgamev10/com.example.myimagematchgamev10.MainActivity”-Android系统。意图行动主-cAndroid。意图类别启动器启动:Intent{act=android.Intent.action.MAIN cat=[android.Intent.category.LAUNCHER]cmp=com.example.myimagematchgamev10/.MainActivity}
从没有日志的情况下我可以看出,您使用的是姜饼之后引入的API。
只要阅读LogCat,它就会给你提示不起作用的确切函数。
我在Android中做了一个简单的计时器应用程序,就像只要过了10sec它就会给出一个消息“完成了!!!”。 在中(计划添加嘟嘟声),同时在另一个文本视图中显示。但我注意到我的应用程序在nexus模拟器上运行得很好,而在pixel模拟器上却崩溃了, 而且我的真手机(红米note 4)也一直崩溃, 这可能是什么原因? 我的代码: XML代码: Logcat错误消息: 图像
我有应用程序上传在玩商店。它在调试模式下运行良好,但当我从play store下载相同的APK(发布模式)时,它就崩溃了。我无法确定堆栈跟踪没有给出准确的错误位置。 java.lang.RuntimeException:在Android.os.AsyncTask$3上完成(asynctask.java:318)在java.util.concurrent.FutureTask.FinishCompl
我是Android开发的新手。我能够使这个应用程序在Android5.1中工作,但是,当我试图在Android4.1中部署这个应用程序时,它在主活动本身就会崩溃。我理解它,因为我使用的是矢量图像和Android4.1不兼容它。为了克服这一点,我设置了如下所述的代码: 我现在收到这个错误 代码文件 XML文件 请帮助我调试这个。我有点被困住了,需要你的帮助。
我创建了一个应用程序,在Android marshmallow中崩溃,而在under版本中我的应用程序正常工作。 这怎么可能?这是我的清单代码: 这是分级代码: 我读到你必须在代码中修改一些东西,使其与AndroidMarshmallow兼容。 我该如何解决问题呢?
我碰壁了,没有帮助我过不去。 我正在尝试使用Java Servlet为我的Android应用程序进行简单的登录和注册,问题是当我在Tomcat/Eclipse上本地运行它时,一切都正常,但当我在Heroku free服务器上部署WAR文件时,数据库部分似乎根本不起作用。 我怀疑这实际上是驱动程序的问题,但我无法获取日志,因为我通过我的Android应用程序连接到它。 我在freesqldataba