要求构造函数的主要活动 - 不在清单文件中的第二个活动

时间:2017-06-17 14:46:03

标签: java android

  enter code here


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

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"

    android:roundIcon="@mipmap/ic_launcher_round"
    android:screenOrientation="landscape"
    android:supportsRtl="true"
   >
    <activity

        android:name=".MainActivity"
       android:screenOrientation="landscape"
    android:theme="@style/AppTheme"
        android:label="@string/app_name">
    </activity>
    <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </application>
    <activity android:name=".SecondActivity" />
    <activity android:name=".ThirdActivity" />
    <activity android:name=".FourthActivity" />
    <activity android:name=".FifthActivity" />
    <activity android:name=".SixthActivity" />
    </manifest>



    -----------------------------------------------------------------------


   package com.example.newuser.applicationtwo;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.util.Log;
    import android.view.View;
    import android.widget.ImageButton;
    import static android.content.ContentValues.TAG;

  public class MainActivity extends Activity implements View.OnClickListener 
{

  private ImageButton playBtn, multiplayerBtn, settingsBtn;

  public MainActivity(ImageButton playBtn, ImageButton multiplayerBtn) {
    this.playBtn = playBtn;
    this.multiplayerBtn = multiplayerBtn;
  }


  @Override
  public void onClick(View v) {
    switch (v.getId()) {


        case R.id.playBtn:
            Intent i = new Intent(getApplicationContext(), 
    SecondActivity.class);

            startActivity(i);
            onClick(playBtn);
            Log.d(TAG, "Play Button Move");

            break;

        case R.id.TopicBtn8:
            Intent j = new Intent(getApplicationContext(), 
  ThirdActivity.class);

            startActivity(j);
            onClick(multiplayerBtn);
            Log.d(TAG, "Multiplayer Button Move");

            break;
        case R.id.settingsBtn:
            Intent k = new Intent(getApplicationContext(), 
    SixthActivity.class);

            startActivity(k);
            onClick(settingsBtn);
            Log.d(TAG, "Settings Button Move");

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

    playBtn = (ImageButton) findViewById(R.id.playBtn);
    multiplayerBtn = (ImageButton) findViewById(R.id.multiplayerBtn);
    settingsBtn = (ImageButton) findViewById(R.id.settingsBtn);

    playBtn.setOnClickListener(MainActivity.this);
    multiplayerBtn.setOnClickListener(MainActivity.this);
    settingsBtn.setOnClickListener(MainActivity.this);



   ------------------------------------------------------------------------


   package com.example.newuser.applicationtwo;

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.util.Log;
 import android.view.View;
 import android.widget.Button;

  import static android.content.ContentValues.TAG;

  class SecondActivity extends AppCompatActivity implements 
   View.OnClickListener {

 private Button topic1Btn, topic2Btn, topic3Btn, topic4Btn, topic5Btn, 
 topic6Btn, topic7Btn, topic8Btn;

 public SecondActivity(Button topic1Btn, Button topic2Btn, Button topic3Btn, 
  Button topic4Btn,
                      Button topic5Btn, Button topic6Btn, Button topic7Btn, 
   Button topic8Btn) {
    this.topic1Btn = topic1Btn;
    this.topic2Btn = topic2Btn;
    this.topic3Btn = topic3Btn;
    this.topic4Btn = topic4Btn;
    this.topic5Btn = topic5Btn;
    this.topic6Btn = topic6Btn;
    this.topic7Btn = topic7Btn;
    this.topic8Btn = topic8Btn;

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {


        case R.id.topic1Btn:
            Intent i = new Intent(getApplicationContext(), 
  SixthActivity.class);

            startActivity(i);
            onClick(topic1Btn);
            Log.d(TAG, "Topic 1 Button Clicked");

            break;

        case R.id.topic2Btn:
            Intent j = new Intent(getApplicationContext(), 
   SixthActivity.class);

            startActivity(j);
            onClick(topic2Btn);
            Log.d(TAG, "Topic 2 Button Clicked");

            break;
        case R.id.topic3Btn:
            Intent k = new Intent(getApplicationContext(), 
    SixthActivity.class);

            startActivity(k);
            onClick(topic3Btn);
            Log.d(TAG, "Topic 3 Button Move");

            break;

        case R.id.topic4Btn:
            Intent l = new Intent(getApplicationContext(), 
   SixthActivity.class);

            startActivity(l);
            onClick(topic4Btn);
            Log.d(TAG, "Topic 4 Button Move");

            break;
        case R.id.topic5Btn:
            Intent m = new Intent(getApplicationContext(), 
    SixthActivity.class);

            startActivity(m);
            onClick(topic5Btn);
            Log.d(TAG, "Topic 5 Button Clicked");

            break;

        case R.id.topic6Btn:
            Intent n = new Intent(getApplicationContext(), 
    SixthActivity.class);
            startActivity(n);
            onClick(topic6Btn);
            Log.d(TAG, "Topic 6 Button Clicked");

            break;
        case R.id.topic7Btn:
            Intent o = new Intent(getApplicationContext(), 
    SixthActivity.class);

            startActivity(o);
            onClick(topic7Btn);
            Log.d(TAG, "Topic 7 Button Move");

            break;

        case R.id.topic8Btn:
            Intent p = new Intent(getApplicationContext(), 
     SixthActivity.class);

            startActivity(p);
            onClick(topic8Btn);
            Log.d(TAG, "Topic 8 Button Move");

            break;
    }
  }

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

    topic1Btn = (Button) findViewById(R.id.topic1Btn);
    topic2Btn = (Button) findViewById(R.id.topic2Btn);
    topic3Btn = (Button) findViewById(R.id.topic3Btn);
    topic4Btn = (Button) findViewById(R.id.topic4Btn);
    topic5Btn = (Button) findViewById(R.id.topic5Btn);
    topic6Btn = (Button) findViewById(R.id.topic6Btn);
    topic7Btn = (Button) findViewById(R.id.topic7Btn);
    topic8Btn = (Button) findViewById(R.id.topic8Btn);

    topic1Btn.setOnClickListener(SecondActivity.this);
    topic2Btn.setOnClickListener(SecondActivity.this);
    topic3Btn.setOnClickListener(SecondActivity.this);
    topic4Btn.setOnClickListener(SecondActivity.this);
    topic5Btn.setOnClickListener(SecondActivity.this);
    topic6Btn.setOnClickListener(SecondActivity.this);
    topic7Btn.setOnClickListener(SecondActivity.this);
    topic8Btn.setOnClickListener(SecondActivity.this);


    Intent intent = new Intent(getApplicationContext(), 
  SixthActivity.class);

    startActivity(intent);
  }
  }

----------------------------------------------------

我想知道是否有人可以提供帮助?我正在尝试建立一个测验应用程序和我 我对清单文件中的主要活动有问题。它似乎 要求一个构造函数,当我运行代码它告诉我 以下:运行SecondActivity时出错:活动&#39; SecondActivity&#39;是 未在AndroidManifest.xml中声明。我不确定如何改变它 不幸的是,MainActivity或者为SecondActivity添加了什么 通过关于MainActivity的类似问题阅读我无法解决它。

我附加了Manifest文件以及MainActivity和SecondActivity

4 个答案:

答案 0 :(得分:1)

移动

</application>*

之前

</manifest>

因为现在只有第一个活动在&lt;申请&gt;范围

答案 1 :(得分:0)

首先删除此处不需要的所有构造函数您没有在清单中的应用程序标记下添加所有活动,还您定义了活动中的启动器过滤器标记将其添加到活动标记下,更改清单如下

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

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:screenOrientation="landscape"
    android:supportsRtl="true"
   >
    <activity
      android:name=".MainActivity"
      android:screenOrientation="landscape"
      android:theme="@style/AppTheme"
      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=".SecondActivity" />
    <activity android:name=".ThirdActivity" />
    <activity android:name=".FourthActivity" />
    <activity android:name=".FifthActivity" />
    <activity android:name=".SixthActivity" />
     </application>
    </manifest>

我还建议你通过基本的android开发教程 https://developer.android.com/training/index.html

答案 2 :(得分:0)

activity代码属于application代码。

这应该有效:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.newuser.applicationtwo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="landscape"
android:supportsRtl="true">
<activity
    android:name=".MainActivity"
    android:screenOrientation="landscape"
    android:theme="@style/AppTheme"
    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=".SecondActivity" />
    <activity android:name=".ThirdActivity" />
    <activity android:name=".FourthActivity" />
    <activity android:name=".FifthActivity" />
    <activity android:name=".SixthActivity" />
</application>
</manifest>

答案 3 :(得分:0)

如果你想首先启动一个完全活动(也就是当应用启动那个确切的活动时),你在androidmanifest文件中声明的所有活动都应该包含在<application> </application>标签中。首先启动,例如,如果你想要一个徽标图像,为它创建一个活动,你可以将类别名称更改为LAUNCHER,如果你不希望它成为应用程序启动时的第一个活动,就把它放到DEFAULT中这个:

<category android:name="android.intent.category.DEFAULT" />