【问题标题】:Android activity manifest errorAndroid 活动清单错误
【发布时间】:2014-06-29 01:47:21
【问题描述】:

我正在尝试运行一个指向 Result.java 的活动,这是我的文件 Tables.java

package com.example.squarecube;

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

public class Tables extends Activity{
TextView t1,t2;
EditText e1;
Button b1;
int a,b,ans;
int score,trials;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);
    trials = 0;
    score = 0;
    t1 = (TextView) findViewById(R.id.text);
    t2 = (TextView) findViewById(R.id.text2);
    e1 = (EditText) findViewById(R.id.ans);
    b1 = (Button) findViewById(R.id.but);
    a= (int)Math.floor(Math.random() * 7) +13;
    b= (int)Math.floor(Math.random() * 9) + 1;
    t1.setText("What is " + a + "*" + b);
    b1.setText("Answer");
    t2.setText("Score : "+ score + "\nTrials : " + trials);
    b1.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View arg0) {
            String no = e1.getText().toString();
            ans = Integer.parseInt(no);
            if (a*b == ans)
            {
                t1.setText("Correct");
                trials++;
                score++;
            }
            else
            {
                t1.setText("Wrong");
                trials++;
            }
            a= (int)Math.floor(Math.random() * 7) +13;
            b= (int)Math.floor(Math.random() * 9) + 1;
            t1.setText("What is " + a + "*" + b);
            t2.setText("Score : "+ score + "\nTrials : " + trials);
            if (trials >= 10)
            {
                Intent abc = new Intent(Tables.this,Result.class);
                Bundle myBun = new Bundle();
                myBun.putInt("score", score);
                abc.putExtras(myBun);
                startActivity(abc);
            }

        }
    });

}



}

这是 android 清单

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.squarecube.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MainActivity" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity
        android:name="com.example.squarecube.Cube"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.Cube" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.squarecube.Tables"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.Tables" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.squarecube.Menu"
        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="com.example.squarecube.Result"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.lAUNCHER" />

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

</manifest>

问题是代码没有切换到Results.java活动,同样的Intent过去对我有用,但我一生都无法理解为什么它没有切换到Results活动

我们将不胜感激您的帮助

【问题讨论】:

  • 所有活动都是启动器?
  • 我想对所有作为启动器的活动提出同样的问题。
  • 那么这个活动叫Result还是Results
  • 是的,除了作为主要活动的菜单

标签: java android android-activity android-manifest


【解决方案1】:

您的清单文件存在多个问题:

1) 首先从您的结果活动中删除它

    <intent-filter>
        <action android:name="android.intent.action.lAUNCHER" />

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

如果这是您从代码内部启动的活动,则不需要。

2) Intent 动作在这里定义:https://developer.android.com/reference/android/content/Intent.html你不需要定义你自己的,比如android.intent.action.Cube

【讨论】:

    【解决方案2】:

    改变这个

     <activity
            android:name="com.example.squarecube.Result"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.lAUNCHER" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    

    <activity
            android:name="com.example.squarecube.Result"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.Result" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    

    以及如何每个活动都是 LAUNCHER 保留一个活动 LAUNCHER,这是您的第一个活动,并将休息设为 DEFAULT

    【讨论】:

      【解决方案3】:

      从清单中删除以下代码:

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

      【讨论】:

        【解决方案4】:

        改变这个

        <activity
            android:name="com.example.squarecube.Result"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.lAUNCHER" />
        
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        

         <activity
            android:name="com.example.squarecube.Result"
            android:label="@string/app_name" >    
        </activity>
        

        除了MainActivity 之外的所有其他人都一样,这可能应该是启动器活动

        http://developer.android.com/guide/components/intents-filters.html

        显式意图指定以名称开头的组件( 完全限定的类名)。您通常会使用显式意图 在您自己的应用程序中启动一个组件,因为您知道类名 您要启动的活动或服务。例如,开始一个新的 响应用户操作或启动服务以下载 文件在后台。

        您不需要意图过滤器。您可以使用显式意图

        【讨论】:

        • 您好,感谢您的回复,但仍然无法正常工作。我已经从除 MAIN 活动之外的每个活动中删除了意图过滤器,但它给了我同样的错误。我想也许是因为 Intent 类,也许我在那里搞砸了一些东西
        • @user1905568 错误是什么?在Result.java 中发布堆栈跟踪可能是一个问题
        • @user1905568 对于 MainActivity 有 &lt;action android:name="android.intent.action.MAIN" /&gt;
        猜你喜欢
        • 1970-01-01
        • 2011-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-20
        • 2019-07-24
        • 1970-01-01
        相关资源
        最近更新 更多