Android中的活动和清单

时间:2013-04-20 12:26:45

标签: android android-activity manifest default

我知道有很多关于此的问题,但我无法弄清楚为什么我的程序会继续加载错误的活动,即使我已经在清单中使用了正确的默认值。这里有一些适合你的代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="swin.examples"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="swin.examples.TemperatureConvertor"
                  android:label="@string/app_name">
        </activity>        
        <activity android:name="swin.examples.FeetToCmConvertor"
                  android:label="@string/app_name">
        </activity>
        <activity android:name="swin.examples.Main" 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> 

如果它不在清单中,我认为问题就在这里:

package swin.examples;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity 
{

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeUI();
    }

private void initializeUI()
{
    Button btnCm = (Button)findViewById(R.id.btnFeet);
    btnCm.setOnClickListener(btnFeetListener);
    Button convertButton = (Button)findViewById(R.id.btnTemp);
    convertButton.setOnClickListener(btnTempListener);

}

/** Handle convert button click */ 
private OnClickListener btnTempListener = new OnClickListener() 
{
    public void onClick(View v) 
    {
            convertButtonClicked();
    }
};

private OnClickListener btnFeetListener = new OnClickListener() 
{
    public void onClick(View v) 
    {
            btnCmClicked();
    }
};

private void btnCmClicked()
{
    // set the sender and the receiver of the intent
    Intent intent = new Intent();
    intent.setClass(getApplicationContext(), swin.examples.FeetToCmConvertor.class);

    startActivity(intent); // transmit your intent

}

private void convertButtonClicked()
{
    // set the sender and the receiver of the intent
    Intent intent = new Intent();
    intent.setClass(getApplicationContext(), swin.examples.TemperatureConvertor.class);

    startActivity(intent); // transmit your intent  
}
}

我无法解决这个问题,希望你能帮助我!非常感谢你!

编辑:代码已更新,但仍无效。如果这有帮助,这是日志消息:

[2013-04-20 22:44:20 - CombinedConvertor] Success!
[2013-04-20 22:44:21 - CombinedConvertor] Starting activity swin.examples.TemperatureConvertor on device emulator-5554
[2013-04-20 22:44:23 - CombinedConvertor] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=swin.examples/.TemperatureConvertor }

2 个答案:

答案 0 :(得分:3)

IntentFilter的正确Activity

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

答案 1 :(得分:0)

有史以来最奇怪的错误。为其他人工作,而不是我。重新设定温度转换器活动,现在可以使用了。谢谢大家的帮助!