安卓小白,课程需求要做一个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>
可以成功跳转。