Android Studio 按钮跳转功能的实现,以及点击后闪退问题的处理

安卓小白,课程需求要做一个APP。在做的时候,做到一个功能:点击按钮,跳转到新的界面。
教程可参考:
https://blog.youkuaiyun.com/baidu_30258569/article/details/49409145
https://blog.youkuaiyun.com/mghhz816210/article/details/50373698
https://blog.youkuaiyun.com/qq_20737519/article/details/50145687

好,我在新的界面的xml文件这样写的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".identify">

    <TextView
    android:text="欢迎来到识别界面,在此修改"/>

</android.support.constraint.ConstraintLayout>

只有一行测试用的文字。看起来好像没什么问题。然后run,点击按钮之后闪退。报错信息如下:

05-22 14:36:41.441 22856-22856/com.example.coder_cxk.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.coder_cxk.myapplication, PID: 22856
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coder_cxk.myapplication/com.example.coder_cxk.myapplication.identify}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: You must supply a layout_width attribute.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

看了很久,觉得没啥问题。这个layout-width似乎也指定了。然后查了很久发现,text的宽度也要指定。于是改成这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".identify">

    <TextView
    android:id="@+id/test_text"
    android:layout_width="wrap_content"
    android:layout_height="73dp"
    android:text="欢迎来到识别界面,在此修改"/>

</android.support.constraint.ConstraintLayout>

可以成功跳转。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值