2012-06-15 78 views
1

我无法从线程启动活动。 我想在2秒后开始一项活动。但它给错误 - 应用程序意外停止。无法从线程启动活动

这是我想要运行线程的活动的代码。

package com.pack.prog; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.Window; 
import android.view.WindowManager.LayoutParams; 
import android.widget.Toast; 

public class Splash extends Activity { 

    MediaPlayer mPlayer; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     try { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, 
        LayoutParams.FLAG_FULLSCREEN); 

      setContentView(R.layout.splash); 

      mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments); 
      mPlayer.start(); 

      Thread thread = new Thread() { 
       public void run() { 

        try { 
         sleep(2000); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         Toast.makeText(Splash.this, e.toString(), 
           Toast.LENGTH_LONG).show(); 
        } finally { 
         Intent i = new Intent("com.pack.prog.StartingMenu"); 
         startActivity(i); 
        } 

       }; 
      }; 
      thread.start(); 

     } catch (Exception e) { 
      Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onPause() { 

     try { 
      super.onPause(); 
      if (mPlayer.isPlaying()) { 
       mPlayer.release(); 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    public void onBackPressed() { 
     try { 
      super.onBackPressed(); 
      if (mPlayer.isPlaying()) { 
       mPlayer.release(); 
      } 
     } catch (Exception w) { 
      Toast.makeText(this, w.toString(), Toast.LENGTH_LONG).show(); 
     } finally { 
      finish(); 
     } 
    } 

} 

这里是清单文件。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.pack.prog" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".Splash" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name=".StartingMenu" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.pack.prog.STARTINGMENU" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 


    </application> 

</manifest> 

我认为只有清单存在一些问题。

+0

后stacktrsce .. – user370305

+0

有eorror? ......... –

+0

blahhh ......我傻 com.pack.prog.StartingMenu 应该 com.pack.prog.STARTINGMENU – reiley

回答

2

我觉得动作名称是不同的充"com.pack.prog.StartingMenu"和清单"com.pack.prog.STARTINGMENU"

,也做,在UI线程.. ...

mPlayer = MediaPlayer.create(Splash.this, R.raw.happy_moments); 
  mPlayer.start(); 
new Handler().postDelayed(new Runnable() { 

       public void run() { 

        Intent i = new Intent("com.pack.prog.STARTINGMENU"); //<--- what ever which is right 
        startActivity(i); 
       } 
      }, 2000); 
+1

烨得到了太感谢 – reiley

+1

我打算发布相同的东西。但我想startActivity也应该从UI线程调用。 – noob

+0

只是合作...... :) –

0

应该使用

runOnUiThread(new Runnable() { 
    public void run() { 
        try { 
         sleep(2000); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         Toast.makeText(Splash.this, e.toString(), 
           Toast.LENGTH_LONG).show(); 
        } finally { 
         Intent i = new Intent("com.pack.prog.StartingMenu"); 
         startActivity(i); 
        } 
    } 
}); 
+1

这个代码将会使用户界面无响应,持续2秒 –

0

你不能从工作者线程中执行UI相关的东西

1

首先你的行为不配套Intent i = new Intent("com.pack.prog.StartingMenu"); 和清单是<action android:name="com.pack.prog.STARTINGMENU" />

严重的问题

可以在螺纹触摸UI部分可以通过封闭与UI交互内螺纹他们根据runOnUiThread

因此,将您的代码更改为此。

Thread thread = new Thread() { 
      public void run() { 

       try { 
        sleep(2000); 
       } catch (Exception e) { 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          Toast.makeText(Splash.this, e.toString(), 
            Toast.LENGTH_LONG).show(); 

         } 
        }); 

       } finally { 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          Intent i = new Intent("com.pack.prog.StartingMenu"); 
          startActivity(i); 

         } 
        }); 

       } 

      }; 
     }; 
     thread.start(); 
0

我更喜欢使用Timer做所有基于时间的任务。请检查下面的实现:

Timer t =new Timer(); 
t.schedule(new TimerTask() { 

    @Override 
    public void run() { 
     Intent i = new Intent("com.pack.prog.StartingMenu"); 
     startActivity(i); 
    } 
}, 2000); 
2

尝试logcat的这个

mediaplayer = MediaPlayer.create(getBaseContext(), R.raw.happy_moments); 
    mediaplayer.start();              
     MyThread myThread=new MyThread(); 
     myThread.start(); 

}     
class MyThread extends Thread 
{ 
    public void run(){ 
     try{          
      Thread.sleep(2000);     
     }        
     catch(Exception ex){ 

      Log.e("Welcome Exception :",ex.toString()); 
     }      
     mHandler.sendEmptyMessage(0);  
    }     
} 
Handler mHandler=new Handler(){ 
    public void handleMessage(Message msg){ 
     super.handleMessage(msg);     

     if () 
     {  
      MyThread myThread=new MyThread(); 
      myThread.start(); 
     } 
     else 
     { 
    Intent i=new Intent(firstactivity.this,secondactivity.class); 
      startActivity(i); 
      finish(); 
     } 
    } 
}; 

}