Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android,onWindowFocusChanged方法导致应用程序崩溃_Android - Fatal编程技术网

Android,onWindowFocusChanged方法导致应用程序崩溃

Android,onWindowFocusChanged方法导致应用程序崩溃,android,Android,这是我的活动代码 public class HomeActivity extends Activity { private String html = ""; private Handler mHandler; private ActionBar actionBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta

这是我的活动代码

public class HomeActivity extends Activity {

    private String html = "";
    private Handler mHandler;
    private ActionBar actionBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        actionBar = (ActionBar) findViewById(R.id.actionbar);
        actionBar.setHomeAction(new IntentAction(this, createIntent(this), R.drawable.ic_title_home_default));
        actionBar.setTitle("someTitle");



        Intent intent = new Intent(this, ComposeActivity.class);

        final Action otherAction = new IntentAction(this, intent, R.drawable.ic_title_share_default);
        actionBar.addAction(otherAction);

        mHandler = new Handler();  
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if(hasFocus)
            checkPrice.start();
    }

    public static Intent createIntent(Context context) {
        Intent i = new Intent(context, HomeActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return i;
    }


    private Thread checkPrice = new Thread() {
        public void run() {
            actionBar.setProgressBarVisibility(View.VISIBLE);
            try {       
               //...some code
                mHandler.post(showUpdate);
            } catch (Exception e) {
            }
        }
    };  

    private Runnable showUpdate = new Runnable(){
        public void run(){
            actionBar.setProgressBarVisibility(View.GONE);
            Toast.makeText(HomeActivity.this, "HTML Code: " + html, Toast.LENGTH_SHORT).show();
        }
    };

}
在添加了
onWindowFocusChanged
方法之后,Intent
Intent
不能再启动了。我发现以下错误:

 12-12 10:50:02.468: W/dalvikvm(540): threadid=3: thread exiting with
 uncaught exception (group=0x4001b188) 12-12 10:50:02.468:
 E/AndroidRuntime(540): Uncaught handler: thread main exiting due to
 uncaught exception 12-12 10:50:02.468: E/AndroidRuntime(540):
 java.lang.IllegalThreadStateException: Thread already started. 12-12
 10:50:02.468: E/AndroidRuntime(540):   at
 java.lang.Thread.start(Thread.java:1322) 12-12 10:50:02.468:
 E/AndroidRuntime(540):     at
 HomeActivity.onWindowFocusChanged(HomeActivity.java:58)
 12-12 10:50:02.468: E/AndroidRuntime(540):     at
 com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:1969)
 12-12 10:50:02.468: E/AndroidRuntime(540):     at
 android.view.View.dispatchWindowFocusChanged(View.java:3731) 12-12
 10:50:02.468: E/AndroidRuntime(540):   at
 android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:657)
12-12 10:50:02.468: E/AndroidRuntime(540):  at
 android.view.ViewRoot.handleMessage(ViewRoot.java:1819) 12-12
 10:50:02.468: E/AndroidRuntime(540):   at
 android.os.Handler.dispatchMessage(Handler.java:99) 12-12
 10:50:02.468: E/AndroidRuntime(540):   at
 android.os.Looper.loop(Looper.java:123) 12-12 10:50:02.468:
 E/AndroidRuntime(540):     at
 android.app.ActivityThread.main(ActivityThread.java:4363) 12-12
 10:50:02.468: E/AndroidRuntime(540):   at
 java.lang.reflect.Method.invokeNative(Native Method) 12-12
 10:50:02.468: E/AndroidRuntime(540):   at
 java.lang.reflect.Method.invoke(Method.java:521) 12-12 10:50:02.468:
 E/AndroidRuntime(540):     at
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 12-12 10:50:02.468: E/AndroidRuntime(540):     at
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-12
 10:50:02.468: E/AndroidRuntime(540):   at
 dalvik.system.NativeStart.main(Native Method)

有没有办法解决这个问题?

您的onWindowFocusChanged对同一个不正确的线程多次调用
Thread.start()
。您可能需要使用像
isStarted
这样的变量来保证只调用一次
Thread.start()
。或者每次启动一个新线程。这取决于程序的逻辑。

您的onWindowFocusChanged对同一个线程多次调用
Thread.start()
,但这是不正确的。您可能需要使用像
isStarted
这样的变量来保证只调用一次
Thread.start()
。或者每次启动一个新线程。这取决于程序的逻辑。

试试这个

   @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if(hasFocus && !checkPrice.isAlive())
            checkPrice.start();
    }
试试这个

   @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if(hasFocus && !checkPrice.isAlive())
            checkPrice.start();
    }

一种方法是新线程(new Runnable(){public void run(){/*将代码放在这里*/});或者它可以是您自己实现Runnable的类。在该类类型的活动中定义一个memeber,然后每次都将该成员传递给新的Thread.start()。不过最好使用Executors Java framework或Android AsyncTask。如果我自己在这里解释的话,解释的质量会很低。最好检查有关异步任务和执行器的文档(即在您对线程执行所需操作之后)。一种方法是新线程(new Runnable(){public void run(){/*将代码放在这里*/});或者它可以是您自己实现Runnable的类。在该类类型的活动中定义一个memeber,然后每次都将该成员传递给新的Thread.start()。不过最好使用Executors Java framework或Android AsyncTask。如果我自己在这里解释的话,解释的质量会很低。最好检查有关异步任务和执行器的文档(即在您对线程执行了所需的操作之后)。