0

我有一个设置了两个活动的基本 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;
}

}

飞溅.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>

谢谢!

4

4 回答 4

5

您忘记MainActivity从Activity 启动SplashActivity。所以开始它:

@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);
}
于 2013-05-28T18:29:55.030 回答
2

您可以尝试用以下代码替换您的 Splash 类:

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 变量表示您希望显示初始屏幕的时间。

于 2013-05-28T18:38:54.347 回答
1

你启动了 Splash Activity,但是你没有任何东西可以启动第二个。您必须创建一个Intent并使用startActivity().

于 2013-05-28T18:30:08.410 回答
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。

笔记

要更改您的应用暂停的时间量,请将 sleep(3000) 更改为您希望它暂停的毫秒数。

此外,您在指定意图时,您的类名应该全部大写,尽管包名应该全部小写。

于 2013-05-28T18:34:46.393 回答