2013-05-28 128 views
0

我有一个基本的Android应用程序设置了两个活动。问题是,第一个打开,并永远停留在那里,第二个不会启动。Android,第二次活动不会启动

任何人都可以说出了什么问题吗?

MainActivity.java

package com.desecrationstudios.firstapp; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

int counter; 
Button add, sub; 
TextView display; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.linear); 
    counter = 0; 
    add = (Button) findViewById(R.id.bAdd); 
    sub = (Button) findViewById(R.id.bSub); 
    display = (TextView) findViewById(R.id.tvDisplay); 

    add.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v){ 
      counter++; 
      display.setText("Your total is " + counter); 
     } 
    }); 

    sub.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v){ 
      counter--; 
      display.setText("Your total is " + counter); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

Splash.java:

package com.desecrationstudios.firstapp; 

import android.app.Activity; 
import android.os.Bundle; 

public class Splash extends Activity{ 

@Override 
protected void onCreate(Bundle splash) { 
    super.onCreate(splash); 
    setContentView(R.layout.splash); 
} 

} 

AndroidManifest.xml中

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <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=".MainActivity" 
     android:label="@string/app_name" > 
    </activity> 

</application> 

</manifest> 

谢谢!

回答

5

你忘记开始MainActivity活动从Splash活动。因此请将其启动为:

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

    // start MainActivity here 

    Intent intent=new Intent(this,MainActivity.class); 
    startActivity(intent); 
} 
+0

这样做会给我两个错误,一个是“构造函数Intent(Splash,MainActivity)未定义”,另一个“不包含MainActivity类型的封闭实例可以在范围内访问” –

+1

请参阅我的答案use'Intent intent = new Intent (this,MainActivity.class);'而不是'new Intent(Splash,MainActivity)' –

+0

这使得它工作,但现在MainActivity在Splash之前启动。 –

1

您启动了Splash Activity,但没有任何内容可以启动第二个。您必须创建一个Intent并使用startActivity()启动它。

1

我看到的问题是你不要从你的飞溅活动意图开始你的MainActivity。

我建议是这样的:

Thread pause = new Thread(){ 
    public void run(){ 
    try { 
     sleep(3000); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    }finally{ 
     Intent i = new Intent("your.path.to.MAINACTIVITY"); 
     startActivity(i); 
     finish(); 
    } 
    } 
}; 
pause.start(); 

您设置的内容视图后添加这是你onCreate方法。 这会暂停您的应用程序3秒钟,然后在暂停后启动您的MainActivity。

注意

要改变的时候你的应用程序暂停变革的睡眠(3000),以毫秒数量,你想让它停下来。

此外,当你指定你的类名应该是全部大写的意图,但包名应该全部小写。

2

您可以尝试使用此代码替换您的飞溅类:

package com.desecrationstudios.firstapp; 
import android.app.Activity; 
import android.os.Bundle; 
public class Splash extends Activity{ 
private static long SLEEP_TIME = 5; 
@Override 
protected void onCreate(Bundle splash) { 
    super.onCreate(splash); 
    setContentView(R.layout.splash); 
    IntentLauncher launcher = new IntentLauncher(); 
    launcher.start(); 
} 
private class IntentLauncher extends Thread { 
    public void run() { 
     try { 
    // Sleeping 
     Thread.sleep(SLEEP_TIME*1000); 
     } catch (Exception e) { 
     Log.e(TAG, e.getMessage()); 
     } 

     // Start main activity 
     Intent intent = new Intent(Splash.this, MainActivity.class); 
     Splash.this.startActivity(intent); 
     Splash.this.finish(); 
    } 
} 
} 

注:SLEEP_TIME变量表示要显示启动画面的时间。

相关问题