【问题标题】:Second activity doesn't start第二个活动没有开始
【发布时间】:2014-05-01 14:24:53
【问题描述】:

我在这里阅读了很多,但没有任何帮助。

正如标题所说,我正在编写一个包含两个活动的简单 android 应用程序。第一个包括一个按钮。通过单击此第二个活动应该被激活。但是什么也没发生。

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.schnitzeljagd"
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.schnitzeljagd.UiActivity"
        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.schnitzeljagd.showArchivements"
        android:label="@string/app_name" >
    </activity>
</application>

</manifest>

我的第一个活动是:

package com.example.schnitzeljagd;

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

public class UiActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ui_activitiy);
    Button next = (Button) findViewById(R.id.button2);
    final Intent intent = new Intent(this, showArchivements.class);
    next.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
    startActivity(intent);  }
    });
    }

@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;
   }


}

我的第二个:

package com.example.schnitzeljagd;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class showArchivements extends Activity{


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



    }

}

有人知道这里可能出了什么问题吗?

对不起,我的英语令人毛骨悚然,感谢您的回答!

【问题讨论】:

    标签: android android-intent android-activity


    【解决方案1】:

    您在 showArchivements 中再次夸大了 uiactivity。

    试试这个:

    public class showArchivements extends Activity{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout. _YOUR_SHOW_ARCHIVEMENTS_LAYOUT_NAME_);
        }
    }
    

    【讨论】:

      【解决方案2】:

      这里的问题是您对Activities 使用相同的布局。实际上第二个Activity 是正常打开的,但是你看不出有什么区别,因为布局是一样的。因此,只需为第二个 Activity 创建另一个布局并在 setContentView 调用中使用它。

      【讨论】:

        【解决方案3】:

        在 showArchivements 活动中将 R.layout.ui_activitiy 更改为其他。

        【讨论】:

          【解决方案4】:

          在按钮所在的活动中将此作为点击脉冲?现在在你的主目录中。

          "您现在所在的活动名称".this, "您要打开的活动名称".class

              public class OnClick implements OnClickListener {   
          
              @Override
              public void onClick(View v) {
                  startActivity(new Intent(**main_activity.this, second_activity.class**));
              }
          }   
          

          也许您可以在第二个活动中的 onCreate 中放置一个 textView,然后您肯定知道单击后需要弹出一些东西;-)

          然后将 onClick 部分(如上所示..)放在您的 onCreateOptionsMenu 之后。并删除 onCreate 中的 onClick 部分。

          您的第一个和第二个活动需要另一个布局 .xml

          试试这个,我希望它有效!当它起作用时,让它知道!将此帖子标记为已解决! :-)

          祝你好运,

          【讨论】:

            【解决方案5】:

            试试这个...

            Thus demo application tutorial 向您展示如何与活动交互,当单击按钮时,从当前屏幕(当前活动)导航到另一个屏幕(另一个活动)。

            UiActivity.java

            package com.example.schnitzeljagd;
            
            import android.os.Bundle;
            import android.app.Activity;
            import android.content.Intent;
            import android.view.Menu;
            import android.view.View;
            import android.widget.Button;
            
            public class UiActivity extends Activity {
            
             private Button next;
            
             @Override
              protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.ui_activity_one);
            
               next = (Button) findViewById(R.id.button2);
            
               next.setOnClickListener(new View.OnClickListener() {
            
                  public void onClick(View v) {
            
                   Intent intent = new Intent(this, showArchivements.class);
                   startActivity(intent); 
                   }
                });
               }
            
              @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;
              }
            }
            

            showArchivements.java

            package com.example.schnitzeljagd;
            
            import android.app.Activity;
            import android.content.Intent;
            import android.os.Bundle;
            
             public class showArchivements extends Activity{
            
            
             @Override
               protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.ui_activity_two);
            
              }
            
             }
            

            Manifest.xml

            <?xml version="1.0" encoding="utf-8"?>
            <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.schnitzeljagd"
              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.schnitzeljagd.UiActivity"
                   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.schnitzeljagd.showArchivements"
                       android:label="@string/app_name" >
                   </activity>
              </application>
            
             </manifest>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-05-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-10-17
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多