簡體   English   中英

從第一個活動轉到第二個活動時出錯

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

我是Andriod編程的新手,我想從第二個活動轉到第三個活動,但是由於應用程序停止,所以它不起作用。 這是錯誤:

android.view.InflateException:二進制XML文件第2行:二進制XML文件第2行:膨脹類Linearlayout時出錯

我不明白,因為我做了同樣的事情以從主要活動轉到第一項活動,並且一切正常。 這是我的代碼:

-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);
    }
}

請幫忙

這是您在xml擁有的LinearLayout而不是Linearlayout ,因此它變成了

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

您的代碼中有錯字。 在第二個活動的布局文件Linearlayout更改為LinearLayout

另外,您的xml中還有一個額外的右tools:context=".SecondActivity">>括號,即>> tools:context=".SecondActivity">> Android Studio應該對此tools:context=".SecondActivity">>抱怨。

您在其中有一個額外的“>”

<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的拼寫錯誤(應該是LinearLayout輸出,而不是Linearlayout ),並且在初始LinearLayout標簽的末尾有一個額外的>

但是,盡管這種方法在技術上會起作用,但我認為如果您熟悉Android導航體系結構組件會更好。 它建議使用單一活動應用程序進行導航,但如果有必要,還提供了在多個活動中進行導航的最佳實踐。 值得深思!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM