綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 程式運行時 停止 (有log)

[複製連結] 查看: 1923|回覆: 12|好評: 0
跳轉到指定樓層
樓主
ArthasL | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2013-10-17 00:39
36碎鑽
本帖最後由 ArthasL 於 2013-10-24 23:25 編輯

log:  
https://apk.tw/forum.php?mod=attachment&aid=Nzc1MzkxfGNiMWQwMTQwY2MzMThiNzRlODcxYzgxZWY2ZjRhZTFmfDE3NTM4ODEzNTU%3D&request=yes&_f=.txt

各位 以下是我的程式 暫時發現是加了2個function後發生的 但不知道那裡出錯了
當我移動棋子大約12-14次左右會運行出錯
懷疑有問題的function:
set_dropped_location()
check_dropped_location()
程式可能有點長 先抱歉了

package com.ArthasFung.ColorBalls;

import java.util.Random;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
       
        RelativeLayout main;
        ImageView imgview;
        private ImageView dropball;
        int[] drawx = {20, 65, 109, 154, 199, 243, 288, 332, 377};
        int[] drawy = {126, 170, 215, 260, 304, 350, 393, 439, 483};
        int[] dropped = new int[999];
        int dropped_xy = 0;
        int[] balls = {R.drawable.red,R.drawable.blue,R.drawable.green,R.drawable.yellow,R.drawable.orange,R.drawable.purple,R.drawable.black};
        int ball_id = 0;
        int click_ball = 0;
        int ball_get_id;
        int move_ball = 0;;
        int random_intx;
        int random_inty;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                for(int loop=0; loop<3; loop++) {
                        startgame();
                }
                final Button menu = (Button)findViewById(R.id.menu);
                menu.setBackgroundResource(R.drawable.menubutton);
                menu.setVisibility(View.GONE);

                final Handler handler=new Handler();
                Runnable runnable=new Runnable(){
                   @Override
                   public void run() {
                           menu.setVisibility(View.VISIBLE);
                   }
                };
                handler.postDelayed(runnable, 1500);
        }
       
        public void startgame() {
                //for(int loop=0; loop<3; loop++) {
                        final ImageView dropball = new ImageView(this);
                        main = (RelativeLayout)findViewById(R.id.main);
                        dropball.setId(ball_id);
                        int random_intballs = new Random().nextInt(balls.length);
                        random_intx = new Random().nextInt(drawx.length);
                        random_inty = new Random().nextInt(drawy.length);

                        set_dropped_location();
                       
                        dropball.setImageResource(balls[random_intballs]);
                                        dropball.setAdjustViewBounds(true);  
                                        dropball.setMaxHeight(35);
                                        dropball.setMaxWidth(35);
                                        dropball.setX(drawx[random_intx]);
                                        dropball.setY(drawy[random_inty]);
                                        main.addView(dropball);
                                        ball_id++;
                       
                        dropball.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                       
                                        if (click_ball != 0 && ball_get_id == dropball.getId()) {
                                                dropball.clearAnimation();
                                                click_ball = 0;
                                                move_ball = 0;
                                        }
                                        else if (click_ball == 0) {
                                                Animation flash = new AlphaAnimation(0.1f, 1.0f);
                                                flash.setDuration(1000);
                                                flash.setRepeatCount(Animation.INFINITE);
                                        flash.setRepeatMode(Animation.REVERSE);
                                        dropball.clearAnimation();
                                        dropball.setAnimation(flash);
                                                flash.start();
                                                Toast.makeText(MainActivity.this,
                                                "Ball ID is" + dropball.getId(), Toast.LENGTH_LONG).show();
                                                ball_get_id = dropball.getId();
                                                click_ball = 1;
                                                move_ball = 1;
                                        }
                                       
                                }
                        });
                       
                //}
                //dropball = new ImageView(this);
        }
       
        public void set_dropped_location(){
                dropped[dropped_xy] = (random_intx + 1) * 10 + random_inty + 1;
                dropped_xy++;
                check_ball_location();
        }
       
        public void check_ball_location(){
                for (int n=0; n<dropped_xy-1; n++) {
                        if ((random_intx + 1) * 10 + random_inty + 1 == dropped[n]) {
                                random_intx = new Random().nextInt(drawx.length);
                                random_inty = new Random().nextInt(drawy.length);
                                dropped_xy--;
                                set_dropped_location();
                        }
                }
        }
       
        public boolean onTouchEvent(MotionEvent event) {
                if (move_ball == 1) {
                        int action=event.getAction();
                        if(action == MotionEvent.ACTION_DOWN) {
                                int rawx=(int) event.getX();
                                int rawy=(int) event.getY();
                                dropball = (ImageView)findViewById(ball_get_id);
                                for (int n=0; n<9; n++){
                                        if (rawx > 42+n*45 && rawx < 78+n*45) {
                                                dropball.setX(drawx[n]+24);
                                                dropped[ball_get_id] = (n + 1) * 10;
                                        }
                                }
                                for (int n=0; n<9; n++){
                                        if (rawy > 149 + n*45 && rawy < 185 + n*45) {
                                                dropball.setY(drawy[n]+24);
                                                dropped[ball_get_id] = dropped[ball_get_id] + n + 1;
                                        }
                                }
                                dropball.clearAnimation();
                                for(int loop=0; loop<3; loop++) {
                                        startgame();
                                }
                                click_ball = 0;
                                move_ball = 0;
                        }
                }
                return super.onTouchEvent(event);
        }
       
        public void onclick_tomenu(View view) {
                Intent intent = new Intent(MainActivity.this, Menu.class);
                Bundle aaa = ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.enteralpha, R.anim.exitalpha).toBundle();
                startActivity(intent, aaa);
                MainActivity.this.finish();
                Button menu = (Button)findViewById(R.id.menu);
                menu.setVisibility(View.GONE);
        }

        public void onclick_restart(View view) {
                Toast.makeText(MainActivity.this,
                                "Not finish", Toast.LENGTH_LONG).show();
                    
        }
       
        public void onBackPressed() {
                System.exit(0);  
                return;
        }
}

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
 樓主| ArthasL | 收聽TA | 只看該作者
發表於 2013-10-20 18:30
有人能幫忙嗎? 那個log 能看到問題嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

板凳
moaii | 收聽TA | 只看該作者
發表於 2013-10-20 21:03
10-16 22:58:56.307:       stack overflow!!!   
應該是記憶體不夠用了!!!   或者call 有looping

評分

參與人數 1幫助 +1 收起 理由
ArthasL + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

地板
 樓主| ArthasL | 收聽TA | 只看該作者
發表於 2013-10-20 22:09
本帖最後由 ArthasL 於 2013-10-20 22:46 編輯

那應該要怎麼解決? 其實我的program 不算是很多東西 為什麼會這樣?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

5
 樓主| ArthasL | 收聽TA | 只看該作者
發表於 2013-10-25 09:29
有大神能救救我嗎?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

6
lkk47 | 收聽TA | 只看該作者
發表於 2013-10-25 10:31
你開debug去跑一變程式碼  我個人覺得是 new Random().nextInt 出問題

評分

參與人數 1幫助 +1 收起 理由
ArthasL + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

7
 樓主| ArthasL | 收聽TA | 只看該作者
發表於 2013-10-25 11:08
不懂開debug..我只是試過在手機跟模擬器裡跑的
但我是加了
set_dropped_location()
check_dropped_location()
這2個才會掛的...
new Random().nextInt 這個有什麼問題/???
如果需要 我把整個project放dropbox 再放上來
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

8
lkk47 | 收聽TA | 只看該作者
發表於 2013-10-25 23:44
由 手機網頁 發佈
你上傳上來有空幫你看一下

評分

參與人數 1幫助 +1 收起 理由
ArthasL + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

9
 樓主| ArthasL | 收聽TA | 只看該作者
發表於 2013-10-26 13:25
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

10
Wen-Chin-Huang | 收聽TA | 只看該作者
發表於 2013-10-30 15:56
打開來棋子根本不會動阿~~~

我是要怎麼測?

check_ball_location 的邏輯很有問題

你最後打算要做甚麼

評分

參與人數 1幫助 +1 收起 理由
ArthasL + 1

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則