1

谁能指导我,我正在尝试创建一个具有启动画面和主菜单活动的简单应用程序。

我已经实现了这两个活动,每当我运行模拟器时,我都可以看到启动画面,但它永远不会进入主菜单活动。

我尝试更改清单中的 XML 文件,如下所示:

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

    <uses-sdk android:minSdkVersion="7" />
    <application android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" android:name=".activity.StartApp">
 <activity 
        android:name=".activity.MainMenuApp"
        android:label="@string/app_name">

    </activity>

        <activity android:name=".activity.SplashApp" 
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

谁能建议为什么主菜单没有出现?

谢谢。

编辑:SplashApp.java

import com.book.R;
public class SplashApp extends Activity {

private DBAdapter mDBAdapter;

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

@Override
protected void onStart() {

    super.onStart();
    mDBAdapter = new DBAdapter(getApplicationContext());

    try{

... 
….      int sec = 1;
        new Handler().postDelayed(new Runnable(){
            public void run(){
                Intent intent = new Intent(SplashApp.this, MainMenuApp.class);
                startActivity(intent);

                finish();
            }
        }, sec * 1000);
    }catch (Exception e){
        e.printStackTrace();
    }
}
}

我发现问题可能出在我的清单文件中,上面写着:

package="com.book"

我现在可以加载启动活动,但它不会进入主菜单页面?

4

1 回答 1

0

如果您的主菜单活动实际上是一个Activity,则还应在androidManifest.xml文件中注明(带有或不带有意图过滤器):

...
<activity android:name=".activity.MainMenu" 
    android:label="@string/main_menu">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
...

如果您在SplashAppActivity 中执行初始化任务,并且应用程序的主要功能位于 中MainActivity,您会发现DEFAULTIntent 过滤器很有用。

edit1:从清单中的标签中删除android:name属性application

edit2:使用SplashApp.this.finish()而不是简单的finish(),因为这里的this指的是处理程序。

SplashApp.java:

package com.book.activity;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import com.book.R;

public class SplashApp extends Activity
{
    private DBAdapter mDBAdapter;

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

    @Override
    protected void onStart()
    {
        super.onStart();
        mDBAdapter = new DBAdapter(getApplicationContext());
        try
        {
            ArrayList<BookBean> books = mDBAdapter.getAllBooks();
            if (books == null)
            {
                InputStream instream = getAssets().open("bookdata.txt");
                BufferedReader bf = new BufferedReader(new InputStreamReader(instream));
                String line;
                while ((line = bf.readLine()) != null)
                {
                    String arr[] = line.split("***");
                    for (int i = 0; i < arr.length; i++)
                    {
                        String temp[] = arr[i].split("*");
                        BookBean book = new BookBean();

                        book.setDescription((temp[0]));
                        book.setName(temp[1]);
                        book.setYearPublished(temp[2]);
                        mDBAdapter.insertBook(book);
                    }
                }
            }
            final int sec = 1;
            new Handler().postDelayed(new Runnable()
            {
                public void run()
                {
                    Intent intent = new Intent(SplashApp.this, MainMenuApp.class);
                    startActivity(intent);
                    SplashApp.this.finish();
                }
            }, sec * 1000);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

androidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.book" android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <application android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name">
        <activity android:name=".activity.MainMenuApp" 
            android:label="@string/app_name" />
        <activity android:name=".activity.SplashApp" 
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
于 2012-05-13T17:41:19.937 回答