简体   繁体   English

从第一个活动转到第二个活动时出错

[英]error when going from 1st activity to 2nd activity

I'm new to Andriod programming and I would like to go from the 2nd activity to the 3rd but it doesn't work because the application stops. 我是Andriod编程的新手,我想从第二个活动转到第三个活动,但是由于应用程序停止,所以它不起作用。 Here is the error: 这是错误:

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class Linearlayout android.view.InflateException:二进制XML文件第2行:二进制XML文件第2行:膨胀类Linearlayout时出错

I don't understand because I did the same to move from the main activity to the first and everything worked. 我不明白,因为我做了同样的事情以从主要活动转到第一项活动,并且一切正常。 Here is my code: 这是我的代码:

-first_activity.xml -first_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstActivity">>

<Button
    android:id="@+id/first_activity_next_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next" />    
</RelativeLayout>

-first_activity.java -first_activity.java

public class FirstActivity extends AppCompatActivity {

private Button nextButton;

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

    nextButton = (Button) findViewById(R.id.first_activity_next_btn);

    nextButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent secondActivity = new Intent(FirstActivity.this, SecondActivity.class);
            startActivity(secondActivity);
        }
    });

    }
}

-second_activity.xml -second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".SecondActivity">>

<TextView
    android:id="@+id/activity_main_title_txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="30dp"
    android:text="2nd activity"/>
</Linearlayout>

-second_activity.java -second_activity.java

public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);
    }
}

Need help please 请帮忙

it is LinearLayout not Linearlayout that you have in your xml so it becomes 这是您在xml拥有的LinearLayout而不是Linearlayout ,因此它变成了

-second_activity.xml -second_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".SecondActivity">

<TextView
    android:id="@+id/activity_main_title_txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="30dp"
    android:text="2nd activity"/>
</LinearLayout>

You have a typo in your code. 您的代码中有错字。 Change Linearlayout to LinearLayout in your Second Activity's layout file 在第二个活动的布局文件Linearlayout更改为LinearLayout

另外,您的xml中还有一个额外的右tools:context=".SecondActivity">>括号,即>> tools:context=".SecondActivity">> Android Studio应该对此tools:context=".SecondActivity">>抱怨。

You have an extra '>' in 您在其中有一个额外的“>”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".SecondActivity">> 

LinearLayout is spelled incorrectly (should be LinearLayout out and not Linearlayout ) and you have an extra > at the end of your initial LinearLayout tag. LinearLayout的拼写错误(应该是LinearLayout输出,而不是Linearlayout ),并且在初始LinearLayout标签的末尾有一个额外的>

However, while this approach will technically work I think it would be better if you familiarize yourself with the Android Navigation Architecture Component . 但是,尽管这种方法在技术上会起作用,但我认为如果您熟悉Android导航体系结构组件会更好。 It recommends a single activity application approach to navigation, but also provides best practices on navigating with multiple activities if necessary. 它建议使用单一活动应用程序进行导航,但如果有必要,还提供了在多个活动中进行导航的最佳实践。 Food for thought! 值得深思!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从第二活动到第一活动获取数据 - How to get data from 2nd Activity to 1st Activity 将第1个活动的值传递给android中的第2个活动 - Pass the value from 1st activity to 2nd activity in android 按下后如何将数据从第二个活动传递到第一个活动? - android - How to pass data from 2nd activity to 1st activity when pressed back? - android 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android 如何在第一个活动中调用第二个活动值? - how to call 2nd activity value in 1st activity? 使用android中的putextra和getextra方法从第二个活动转到第一个活动时图像视图的可见性消失了 - visibility gone of image view when come from 2nd activity to 1st activity using putextra and getextra method in android 从第一个活动中获取意图后如何刷新第二个活动 - How to refresh 2nd activity after intent from 1st activity 从第一个活动拨打电话后,第二个活动没有通话 - 2nd Activity doesn't call after calling from 1st Activity 如何保存从第一个活动传递到第二个活动的意图数据 - how to save intent data passed from 1st activity to 2nd activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM