当我更改设备方向或点击主页时,为什么我的应用程序崩溃? [英] Why my app crashing when I change device orientaion or click Home?

查看:96
本文介绍了当我更改设备方向或点击主页时,为什么我的应用程序崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过手机编写了很多用于图形远程控制的代码,我的应用程序使用SurfaceView,画布和套接字,但我有2个错误:

1)单击Home按钮时应用程序崩溃。 br />
2)当我改变设备方向时应用程序崩溃。

你能指出我正确的方向吗?请帮忙。



编辑:第二个问题解决了:)



I wrote a lot of code for graphical remote control via mobile, my app uses SurfaceView, canvas and sockets, but I got 2 errors:
1)Application crashing when I click Home button.
2)Application crashing when I change device orientation.
Could you point me right direction? Please help.

second problem solved:)

package com.example;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.ArrayList;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    public boolean _run = false;
    Panel pnl;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        pnl = new Panel(this);
        setContentView(pnl);
    }
    class Panel extends SurfaceView implements SurfaceHolder.Callback {
        private TutorialThread _thread;

        public Panel(Context context) {
            super(context);
            getHolder().addCallback(this);
            _thread = new TutorialThread(getHolder(), this);
            setFocusable(true);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
                if (event.getAction() == MotionEvent.ACTION_MOVE) {
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                }
                if (event.getAction() == MotionEvent.ACTION_UP) {
                }
                return true;
            }
        }

        @Override
        public void onDraw(Canvas canvas) {
            canvas.drawColor(Color.BLACK);
            Paint mPaint = new Paint();
            //int color = 0xaf000000;
            mPaint.setColor(Color.WHITE);
            mPaint.setStrokeWidth(2);
            canvas.drawText("something", 50, 50, mPaint);
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            _thread.setRunning(true);
            _thread.start();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // simply copied from sample application LunarLander:
            // we have to tell thread to shut down & wait for it to finish, or else
            // it might touch the Surface after we return and explode
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }
    }
    class TutorialThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private Panel _panel;
        //public boolean _run = false;

        public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }

        public void setRunning(boolean run) {
            _run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (_run) {
                try{
                    Thread.sleep(100);
                }catch(Exception x){}
                c = null;
                try {
                    //if(drawn == false)
                    {
                        //]drawn = true;
                        try{
                        c = _surfaceHolder.lockCanvas(null);
                        synchronized (_surfaceHolder) {
                            _panel.onDraw(c);
                        }
                        }catch (Exception cc)
                        {
                            cc.toString();
                        }
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
                //catch (Exception cx)
                  //      {}
            }
        }
    }
}



清单:


Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

          package="com.example"

          android:versionCode="1"

          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"

                  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>



来自LogCat:

java.lang.IllegalThreadStateException:线程已经启动。

如何解决?请帮忙!


From LogCat:
java.lang.IllegalThreadStateException: Thread already started.
How to fix? Please help!

推荐答案

你的清单看起来像什么?你的意思是家里的主菜单或家里的机器人主屏幕吗?确保您的启动器已定义。
whats your manifest look like? do you mean home as in your main menu or home as in androids home screen? make sure your launcher is defined.


感谢所有,我通过插入一行代码解决了这个问题。
Thanks to all, I solved it myself by inserting one line of code.


这篇关于当我更改设备方向或点击主页时,为什么我的应用程序崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆