[英]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.