0

好的,所以我在让我的应用程序工作时遇到问题。基本上我有一个游戏需要从用户那里获取一些图片和字符串。我有一个打开屏幕 (OpeningScreen),它充当打开菜单 (MenuScreen) 的启动屏幕。从那里用户可以选择去游戏或去显示当前图片的活动(PickScreen)。用户可以转到该活动并从那里打开另一个活动,该活动提供他们当前选择的图片的更大版本或默认图片(PicOne)。在这里,用户可以选择拍摄新照片并更改当前字符串。在大多数情况下,这一切都很好。我的问题发生在:

在用户选择一张图片并退出应用程序之后。下次他们打开它时,它会在我返回 PickScreen 或在拍摄新照片后按完成后强制关闭,有时当我去 PicOne 活动时会强制关闭。它不会每次都做同样的事情,它只是在其中一个点崩溃。

当我更改 3 个字符串名称时,会发生另一个问题。按下保存并返回 PickScreen 后,应用程序在返回 PicOne 时崩溃,或者如果我从 MenuScreen 转到 PickScreen 时退出应用程序崩溃。

我知道这是要查看的大量代码,但是我花了很多时间环顾四周并从不同地方获取此应用程序的代码,但我现在无法弄清楚。我想有很多人比我有更多的知识,所以我请求你的帮助。我知道你不能只问一个问题而不表明你一直在做任何工作,所以就在这里。

为什么可能应用程序一次可以完美运行,然后第二次在各个位置崩溃?顺便说一句,在强制关闭后它确实可以正常工作,而且只有一次。为什么当我更改字符串时它会强制关闭?

感谢大家!!

PicOne 课程

public class PicOne extends Activity implements OnClickListener {
ImageView iv;
EditText c1, c2, c3;
Button cam, save;
Bitmap bit, bmp,other;
Intent i;
Uri uriSavedImage;
String imageFilePath10 = "", name1="", name2="", name3="";
final static int cameraData = 0; 
boolean CAMERA;
int camORgal10 = 0;
SharedPreferences gameData;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.picone);
        CAMERA = false;
        iv = (ImageView)findViewById(R.id.picIV);
        cam = (Button)findViewById(R.id.camButton);
        save = (Button)findViewById(R.id.savebut);
        e1 = (EditText)findViewById(R.id.Enter1);
        e2 = (EditText)findViewById(R.id.Enter2);
        e3 = (EditText)findViewById(R.id.Enter3);

        cam.setOnClickListener(this);
        save.setOnClickListener(this);
}
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        //camera
        case R.id.camButton:
            camORgal10 = 1;
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);               
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MySpot"); 
            imagesFolder.mkdirs(); // <---- 
            String fileName = "image_1.PNG"; 
            File output = new File(imagesFolder, fileName);             
            uriSavedImage = Uri.fromFile(output);         
            i.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivityForResult(i, cameraData);
            break;

        case R.id.savebut:
            CAMERA = true;
            name1 = e1.getText().toString();
            name2 = e2.getText().toString();
            name3 = e3.getText().toString();
            SharedPreferences.Editor editor = gameData.edit();
            editor.putInt("NUM10CAMGAL", camORgal10);
            editor.putString("NUM10NAME1", name1);
            editor.putString("NUM10NAME2", name2);
            editor.putString("NUM10NAME3", name3);
            editor.commit();
            Intent goPT = new Intent(this, PickScreen.class);
            goPT.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            goPT.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            finish();
            startActivity(goPT);                
            break;

        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            CAMERA = true;
            name1 = e1.getText().toString();
            name2 = e2.getText().toString();
            name3 = e3.getText().toString();
            SharedPreferences.Editor editor = gameData.edit();
            editor.putInt("NUM10CAMGAL", camORgal10);
            editor.putString("NUM10NAME1", name1);
            editor.putString("NUM10NAME2", name2);
            editor.putString("NUM10NAME3", name3);
            editor.commit();
            Intent goPT = new Intent(this, PickScreen.class);
            goPT.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            goPT.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            finish();
            startActivity(goPT);

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }   

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == cameraData)     
        {         
            if(resultCode == RESULT_OK && data.hasExtra("data"))         
            {             
                    bmp = (Bitmap) data.getExtras().get("data");
                    iv.setImageBitmap(bmp); 



            }         
            else if (resultCode == RESULT_CANCELED)        
            {            
                Toast.makeText(getApplicationContext(), "Cancelled",Toast.LENGTH_SHORT).show();    
            }      
        }


      }



    @Override
    protected void onPause() {
        // TODO Auto-generated method stub

        super.onPause();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub


        if(OpeningScreen.isEXIT)
        {
            finish();
        }
        gameData = getSharedPreferences(MenuScreen.MYFOLDER, 0);
        name1 = slotData.getString("NUM10NAME1", "one");
        name2 = slotData.getString("NUM10NAME2", "two");
        name3 = slotData.getString("NUM10NAME3", "three");
        e1.setText(name1);
        e2.setText(name2);
        e3.setText(name3);
        camORgal10 = gameData.getInt("NUM10CAMGAL", 0);
        if(camORgal10 == 0)
        {
            bit = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        }
        else if(camORgal10 == 1)
        {
            File imgFile = new File(Environment.getExternalStorageDirectory() + "/MySpot/image_1.PNG");
            if(imgFile.exists())
            {
               bit = BitmapFactory.decodeFile(imgFile.getAbsolutePath());   
            }
            else
            {
                bit = BitmapFactory.decodeResource(getResources(), R.drawable.red);
            }
        }

        else
        {
            bit = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        }           
        iv.setImageBitmap(bit);
        super.onResume();
    }
}

开屏

public class OpeningScreen extends Activity {
/** Called when the activity is first created. */
 public static boolean isEXIT = false;

@Override   
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    isEXIT = false;
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(2500);
            } catch(InterruptedException e){

            } finally{
                Intent toMenu = new Intent(getApplicationContext(), MenuScreen.class);
                toMenu.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //toMenu.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                finish();
                startActivity(toMenu);                  
            }               
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    finish();
    super.onPause();        
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    if(isEXIT)
    {
        finish();
    }

    super.onResume();
}    
}

菜单屏幕

public class MenuScreen extends Activity implements OnClickListener {
float x,y;
int camORgal = 0;
ImageButton play, edit, more;
Intent i;
public static String MYFOLDER = "GAMEDATA";
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    play = (ImageButton)findViewById(R.id.IBplay);
    edit = (ImageButton)findViewById(R.id.IBedit);
    more = (ImageButton)findViewById(R.id.IBmore);
    play.setOnClickListener(this);
    edit.setOnClickListener(this);
    more.setOnClickListener(this);
}

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        OpeningScreen.isEXIT = true;
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.IBplay:
        i = new Intent(getApplicationContext(), TheGame.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(i);           
        break;
    case R.id.IBedit:
        i = new Intent(this, PickScreen.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(i);           
        break;
    case R.id.IBmore:

        break;
    }
}

}

选择屏幕

public class PickScreen extends Activity implements OnClickListener {
Button bPic1, bPic2, bPic3;
ImageView ivpic3,ivpic2, ivpic1;
TextView TVpic3a, TVpic3b, TVpic3c, TVpic2a, TVpic2b, TVpic2c, TVpic1a, TVpic1b, TVpic1c;
Intent pageMove;
SharedPreferences gameData;
int camORgal10 = 0;
String threeNamea = "", threeNameb = "", threeNamec = "", twoNamea = "", twoNameb = "", twoNamec = "", oneNamea = "", oneNameb = "", oneNamec = "";

Bitmap bmp1, bmp2,bmp3;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.paytable);
                intitializeThings();        
}


public void intitializeThings()
{
    bPic1 = (Button)findViewById(R.id.pic1but);
    bPic2 = (Button)findViewById(R.id.pic2but);
    bPic3 = (Button)findViewById(R.id.pic3but);
    ivpic3 = (ImageView)findViewById(R.id.ivpic3a);
    ivpic2 = (ImageView)findViewById(R.id.ivpic2a);
    ivpic1 = (ImageView)findViewById(R.id.ivpic1a);
    TVpic3a = (TextView)findViewById(R.id.pic3TVa);
    TVpic3b = (TextView)findViewById(R.id.pic3TVb);
    TVpic3c = (TextView)findViewById(R.id.pic3TVc);
    TVpic2a = (TextView)findViewById(R.id.pic2TVa);
    TVpic2b = (TextView)findViewById(R.id.pic2TVb);
    TVpic2c = (TextView)findViewById(R.id.pic2TVc);
    TVpic1a = (TextView)findViewById(R.id.pic1TVa);
    TVpic1b = (TextView)findViewById(R.id.pic1TVb);
    TVpic1c = (TextView)findViewById(R.id.pic1TVc);


    bPic1.setOnClickListener(this);
    bPic2.setOnClickListener(this);
    bPic3.setOnClickListener(this);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.pic1but:
        pageMove = new Intent(getApplicationContext(), PicOne.class);
        pageMove.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        pageMove.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //pageMove.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        finish();
        startActivity(pageMove);

        break;
    case R.id.pic2but:
        pageMove = new Intent(getApplicationContext(), PicTwo.class);
        pageMove.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //pageMove.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(pageMove);
        finish();
                          break;
    case R.id.pic3but:
        pageMove = new Intent(getApplicationContext(), PicThree.class);
        pageMove.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //pageMove.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(pageMove);
        finish();   
                          break;            
    }
}

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
        Intent goOP = new Intent(this, MenuScreen.class);
        goOP.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        goOP.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        finish();
        startActivity(goOP);

        return true;
    }
    return super.onKeyDown(keyCode, event);
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();   


        gameData = getSharedPreferences(MenuScreen.MYFOLDER, 0);
        oneNamea = gameData.getString("NUM10NAME1", "one");
        oneNameb = gameData.getString("NUM10NAME2", "two");
        oneNamec = gameData.getString("NUM10NAME3", "three");
        camORgal10 = gameData.getInt("NUM10CAMGAL", 0);


        if(camORgal10 == 1)
        {
            File pic1 = new File(Environment.getExternalStorageDirectory() + "/MySpot/image_1.PNG");
            if(pic1.exists())
            {
                bmp1 = BitmapFactory.decodeFile(pic1.getAbsolutePath());
            }
            else
            {
                bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.red);
            }
        }

        else if(camORgal10 == 0)
        {
            bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        }
        else
        {
            bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.red);
        }


        File pic2 = new File(Environment.getExternalStorageDirectory() + "/MySpot/image_2.PNG");
        File pic3 = new File(Environment.getExternalStorageDirectory() + "/MySpot/image_3.PNG");


        if(pic2.exists())
        {
            bmp2 = BitmapFactory.decodeFile(pic2.getAbsolutePath());
        }
        else
        {
            bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.purple);
        }
        if(pic3.exists())
        {
            bmp3 = BitmapFactory.decodeFile(pic3.getAbsolutePath());
        }
        else
        {
            bmp3 = BitmapFactory.decodeResource(getResources(), R.drawable.green);
        }


        ivpic3.setImageBitmap(bmp3);

        ivpic2.setImageBitmap(bmp2);

        ivpic1.setImageBitmap(bmp1);

        TVpic1a.setText(oneNamea);
        TVpic1b.setText(oneNameb);
        TVpic1c.setText(oneNamec);

}


} 
4

2 回答 2

1

Logcat 会给你一个堆栈跟踪,然后使用调试来查明它崩溃的地方。通过阅读代码(尤其是 OOP 代码)来调试现代应用程序几乎是不可能的。

于 2012-03-11T04:06:01.813 回答
0

这可能是因为 AOS 并没有真正关闭应用程序,但您可能认为是。因此,在下一次启动时,AOS 不会“从头开始”启动您的应用程序,而是会启动您的不死缓存应用程序。而且由于您的逻辑没有预料到您的应用程序崩溃。我很确定它在崩溃后再次启动正常,但下一次启动再次崩溃 - >循环。因此,为避免这种情况,请使用 System.exit(0) (大多数高级开发人员会说这是一种不好的做法)来确保您的应用程序不会变成僵尸或更改应用程序的逻辑,使其在下次启动时不会崩溃,因为主要活动是还在那里。

于 2013-07-10T19:30:57.030 回答