当它尝试启动时应用程序崩溃意图

问题描述:

我有一个启动画面,我想在我的主应用程序屏幕之前运行。但是,当计时器结束时,应用程序崩溃。任何想法为什么发生这种情况?提前致谢。当它尝试启动时应用程序崩溃意图

下面是引用的代码

public class Splash extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splash); 

     Thread timer = new Thread() { 
      // Whatever is enclosed in the {} of method run(), runs when we 
      // start the application 
      public void run() { 
       try { 
        sleep(2000); 

       } catch (InterruptedException e) { 
        e.printStackTrace(); 

       } finally { 

        Intent openMainScreen = new Intent("com.package.Main_Screen"); 
        startActivity(openMainScreen); 

       } 
      } 
     }; 

     timer.start(); 
    } 
} 
+3

发表您的logcat错误报告 –

你为什么不干脆用这种意向,

Intent openMainScreen = new Intent(Splash.this,Main_Screen.class); 
startActivity(openMainScreen); 

,并且确保你已在你的清单这样添加的活动,

<activity android:name=".Main_Screen"> 
</activity> 
+0

你知道,我试图把它称为这样,我得到一个错误。我不得不按照你的建议改变我的清单,以便像这样调用它意图openMainScreen = new Intent(Splash.this,Main_Screen.class);谢谢 – Pantheo

+0

没有问题..你是weolcome :) –

下面写代码

Intent openMainScreen = new Intent(this, MainActivity.class); 
startActivity(openMainScreen); 

代替

Intent openMainScreen = new Intent("com.package.Main_Screen"); 
startActivity(openMainScreen); 

,并宣布你的MainActivity到AndroidManifest.xml文件。

<activity 
    android:name=".MainActivity" 
    android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

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

它会解决你的问题。

+0

谢谢。因为它实际上我不得不改变我的清单以及 – Pantheo

+0

@Pantheo看到我编辑的答案。 –

要调用startActivity来自不同的Thread。您必须从UIthread运行它。您试图实现的目标可以通过以下方式轻松完成:

public class Splash extends Activity { 
     Handler handler; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_splash); 
      handler = new Handler(); 
      handler.postDelayed(new Runnable() { 
       public void run() { 
        Intent openMainScreen = new Intent(Splash.this, 
          Main_Screen.class); 
        startActivity(openMainScreen); 

       } 
      }, 2000); 
     } 
    } 

你有这样

Intent openMainScreen = new Intent(ClassName.this, MainActivity.class); 
startActivity(openMainScreen); 

打电话,你必须在清单文件将其注册

<activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" >