简体   繁体   中英

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. Here is the error:

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class 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

<?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

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

<?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

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

-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

另外,您的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.

However, while this approach will technically work I think it would be better if you familiarize yourself with the Android Navigation Architecture Component . It recommends a single activity application approach to navigation, but also provides best practices on navigating with multiple activities if necessary. Food for thought!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM