在尝试运行该应用时,它显示该应用已停止。
这是activity_main.xml页面
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
class="com.example.android.mydemofragment2.FoodListFragment"
/>
<FrameLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
class="com.example.android.mydemofragment2.MenuDetailFragment"
android:id="@+id/fragment_container"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
这是MainActivity页面
package com.example.android.mydemofragment2;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements
FoodListFragment.FoodListListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void itemclicked(long id) {
MenuDetailFragment details = new MenuDetailFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
details.setMenuID(id);
ft.replace(R.id.fragment_container, details);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
这是FoodListFragment页面
package com.example.android.mydemofragment2;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FoodListFragment extends ListFragment {
static interface FoodListListener{
void itemclicked(long id);
}
private FoodListListener listener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.listener = (FoodListListener) activity;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
if(listener!=null){
listener.itemclicked(id);
}
super.onListItemClick(l, v, position, id);
}
public FoodListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String names [] = new String[Menu.menus.length];
for(int i = 0; i<names.length;i++){
names[i]=Menu.menus[i].getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(inflater.getContext(),android.R.layout.simple_list_item_1,names);
setListAdapter(adapter);
return super.onCreateView(inflater,container,savedInstanceState);
}
}
这是Menu java类
package com.example.android.mydemofragment2;
public class Menu {
private String name;
private String description;
public static final Menu[] menus = {
new Menu("Breakfast","2 Whole eggs \n Bread \n Coffee"),
new Menu("Lunch","3 Whole eggs \n Brocolli"),
new Menu("Dinner","Brown rice \n Potato \n Brocolli")
};
private Menu(String name, String description){
this.name = name;
this.description = description;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
@Override
public String toString() {
return this.name;
}
}
这是fragment_menu_detail页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MenuDetailFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/desc"/>
</LinearLayout>
这是MenuDetailFragment页面
package com.example.android.mydemofragment2;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MenuDetailFragment extends Fragment {
private long menuID;
public void setMenuID(long menuID){
this.menuID = menuID;
}
public MenuDetailFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(savedInstanceState != null){
menuID = savedInstanceState.getLong("menuID");
}
return inflater.inflate(R.layout.fragment_menu_detail, container,
false);
}
@Override
public void onStart() {
super.onStart();
View view = getView();
if(view!= null){
TextView title = (TextView) view.findViewById(R.id.name);
TextView description = (TextView) view.findViewById(R.id.desc);
Menu menu = Menu.menus[(int) menuID];
title.setText(menu.getName());
description.setText(menu.getDescription());
}
}
}
请帮助我找出此应用代码中的错误,我尝试了很多,但未能找到我所犯的错误。
整个日志消息
09-03 20:36:35.798 7514-7514 /? I / zygote:不延迟启用-Xcheck:jni(已启用) 09-03 20:36:35.887 7514-7514 /? W / zygote:使用默认值的x86出现意外的X86 CPU变体 09-03 20:36:36.651 7514-7514 / com.example.android.mydemofragment2 I / InstantRun:启动即时运行服务器:是主进程 09-03 20:36:36.925 7514-7514 / com.example.android.mydemofragment2 D / AndroidRuntime:关闭VM 09-03 20:36:36.931 7514-7514 / com.example.android.mydemofragment2 E / AndroidRuntime:FATAL EXCEPTION:main 流程:com.example.android.mydemofragment2,PID:7514 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.android.mydemofragment2 / com.example.android.mydemofragment2.MainActivity}:android.view.InflateException:二进制XML文件行#12:二进制XML文件行#12 :错误夸大类片段 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6494) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 原因:android.view.InflateException:二进制XML文件第12行:二进制XML文件第12行:夸大类片段的错误 原因:android.view.InflateException:二进制XML文件行#12:错误夸大类片段 引起原因:java.lang.IllegalArgumentException:二进制XML文件第12行:必须指定唯一的android:id,android:tag或具有com.example.android.mydemofragment2.FoodListFragment的ID的父对象 在android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3687) 在android.support.v4.app.FragmentController.onCreateView(FragmentController.java:114) 在android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374) 在android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39) 在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:68) 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780) 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:863) 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 在android.view.LayoutInflater.rInflate(LayoutInflater.java:866) 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 在android.view.LayoutInflater.inflate(LayoutInflater.java:515) 在android.view.LayoutInflater.inflate(LayoutInflater.java:423) 在android.view.LayoutInflater.inflate(LayoutInflater.java:374) 在android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 在com.example.android.mydemofragment2.MainActivity.onCreate(MainActivity.java:12) 在android.app.Activity.performCreate(Activity.java:7009) 在android.app.Activity.performCreate(Activity.java:7000) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6494) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)