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