zoukankan      html  css  js  c++  java
  • Android学习笔记之Android Studio添加新的Activity

    1.创建Android项目工程:AndroidTest

    创建过程可参考网上诸多教程。

    2.添加新的Activity,步骤如下

    a. 在layout文件夹上右键,New-Activity-相应Activity,(如Basic Activity).

    b. 配置Activity

    填写Activity Name,Layout Name,Title,Package name等。

    注意:Package name需填写项目包名称,如com.androidtest(初学,不知道对不对,请指正).

    c. 修改activity_second.xml和content_second.xml

    如添加一个TextView,下面是content_second.xml的代码,我认为activity_second.xml应该是layout的一个框架,而content_second.xml为该框架的内容界面.

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/content_second"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.androidtest.SecondActivity"
        tools:showIn="@layout/activity_second">
        <TextView
            android:text="The Second Activity"
            android:id="@+id/textView"
            app:layout_anchor="@+id/include"
            app:layout_anchorGravity="center_vertical|left"
            android:layout_gravity="center_vertical|right"
            android:layout_height="42dp"
            android:layout_width="match_parent"
            android:textSize="30sp" />
    </RelativeLayout>
    复制代码

     d.到此Activity就添加好了,下面是预览界面

    3.Activity之间的跳转

    a.在activity_main.xml添加按钮

    代码如下:

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Sencond Activity"
        android:onClick="onClick"/>

    注意:因android:text="Sencond Activity"采用了硬编码字符串,所以AS给了提示(超智能),此处先不修改了,提示如下:

    b.在MainActivity添加Button的onClick事件处理

    复制代码
    /**
     * 按钮Sencond Activity 事件处理
     */
    public void onClick(View view)
    {
        try
        {
            startActivity(new Intent("com.AndroidTest.SecondActivity"));
        }
        catch (Exception ex)
        {
         // 显示异常信息 Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show(); } }
    复制代码

     4.运行及异常处理

    a. 异常信息

    添加好了Activity,代码也写好了,接下来就是运行了,结果悲剧了,出现了异常:No Activity found to handle Intent { act=com.AndroidTest.SecondActivity }

    见下图:

    b. 解决办法

    Intent分为显示Intent和隐式Intent;使用显示Intent时,可以不添加<intent-filter>标签,但是隐式Intent必须添加<intent-filter>标签,所以解决方法有两种:

    第一种:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    //将3.b中的代码修改为显示Intent
    public void onClick(View view)
    {
        try
        {
            //startActivity(new Intent("com.AndroidTest.SecondActivity"));//隐式intent
            Intent intent = new Intent(this, SecondActivity.class);//显示intent
            startActivity(intent);
        }
        catch (Exception ex)
        {
            // 显示异常信息
            Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    第二种:

    修改AndroidManifest.xml中SecondActivity部分,添加<intent-filter>标签.

    原代码:

    <activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second"
        android:theme="@style/AppTheme.NoActionBar"></activity>

    修改后代码:

    复制代码
    <activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="com.AndroidTest.SecondActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    复制代码

    c. 异常解释

    每一个通过startActivity()方法发出的隐式Intent都至少有一个category,就是 "android.intent.category.DEFAULT",

    所以只要是想接收一个隐式Intent的Activity都应该包括"android.intent.category.DEFAULT" category,不然将导致 Intent 匹配失败。

    参见:Android关于No Activity found to handle Intent的问题

    d. 最终运行图

  • 相关阅读:
    C#导出数据—使用Word模板书签的使用
    C#动态调用泛型类、泛型方法
    C#中运算符的介绍和使用
    C#中自定义类型转换
    Linux删除文件后没有释放空间
    Linux下用dd命令测试硬盘的读写速度
    强制关闭或重启Linux系统的几种方法
    前端使用a标签启动本地.exe程序
    Linux 命令别名,让alias永久生效
    Linux 自动删除N小时或分钟前的文件
  • 原文地址:https://www.cnblogs.com/edensyd/p/8621670.html
Copyright © 2011-2022 走看看