在Android中运行新活动的问题? [英] Problems in Running a new activity in Android?

查看:98
本文介绍了在Android中运行新活动的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,我在youtube上关注了newboston的教程。我的目标是设置一个新活动并运行它,同时将原始活动作为默认活动。我一步一步地遵循教程,由于某些原因,即使我正确地执行所有操作,当程序运行时,它运行mainactivity(我将其设为默认值)并且不运行我做main的活动。我不知道我的问题是什么,我已经被困了几天但是无法找到解决方案。请任何人帮助我解决我的问题。谢谢

我在Windows操作系统上使用Intellij Idea。我真的被困了,因为没有解决这个问题我不能转到下一个教程。



这是必需的信息 -

1.我有两个.xml文件,一个是mail.xml,另一个是web.xml 。

2.我有两个java类,一个是MyActivity.java,另一个是Textplay.java

3.最后但并非最不重要的AndroidManifest.xml



以下是文件。



main.xml



I am new to android and i was following the tutorials of the newboston on youtube. My goal was to set up a new activity and run it while making the original activity a default activity. I followed the tutorial step by step and for some reason even though i do everything correctly, when the program runs,it runs the mainactivity(which i made it default) and doesnt run the activity that i made "main". I dont know what my problem is and i have been stuck for days but cant figure out a solution. Please can anyone help me in solving my problem. Thanks
I am using the Intellij Idea on a windows OS. I am really stuck because without solving this issue i cant move to the next tutorial.

This are the required information-
1. I have two .xml files one is mail.xml and the other is web.xml.
2.I have two java class one is MyActivity.java and the other is Textplay.java
3. Last but not the least the AndroidManifest.xml

Here are the files.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

                android:layout_width="fill_parent"

                android:layout_height="fill_parent"

                android:paddingLeft="16dp"

                android:paddingRight="16dp" >



</RelativeLayout>







web.xml






web.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:orientation="vertical"

              android:layout_width="match_parent"

              android:layout_height="match_parent" android:background="@drawable/android">

</LinearLayout>





MyActivity.java





MyActivity.java

package com.example.androidproject;

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

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





Textplay.java





Textplay.java

package com.example.androidproject;

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

/**
 * Created by user on 05-01-2015.
 */
public class Textplay extends Activity {
    @Override
    protected void onCreate(Bundle flash) {
        super.onCreate(flash);
        setContentView(R.layout.web);
    }
}



AndroidManifest.xml






AndroidManifest.xml


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

          package="com.example.androidproject"

          android:versionCode="1"

          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8"/>
    <application android:label="@string/app_name">

        <activity android:name=".Textplay"

                  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="MyActivity"

                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.androidproject.MYACTIVITY"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>
</manifest> 

推荐答案

你错过了一个。对于android:name属性:

You are missing a "." for the android:name attribute:
<activity android:name=".MyActivity"
          android:label="@string/app_name">



您的应用应该使用Textplay活动启动。要从Textplay启动MyActivity,请在Textplay.java中使用



You app should launch with the Textplay activity. To start the MyActivity from Textplay,
in the Textplay.java:

Intent intent = new Intent("com.example.androidproject.MYACTIVITY");
startActivity(intent);



+++++++ +第2轮+++++++>
试试这个,在onCreate()中创建一个新的Thread,让它睡5秒,然后启动MyActivity:


+++++++ Round 2 +++++++
Try this, create a new Thread in the onCreate(), let it sleep for 5 seconds, then start the MyActivity:

Thread thread = new Thread(){
  public void run(){
     try {
        sleep(5000); // sleep for 5000 milliseconds
     } catch (InterruptedException e) {
        e.printStackTrace();
     } finally {

        Intent intent = new Intent("com.example.androidproject.MYACTIVITY");
        startActivity(intent);
     }
  }
};
thread.start();


这篇关于在Android中运行新活动的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆