我知道这是一个重复的问题,但其他帖子中的建议答案都没有帮我解决这个错误。所以请帮我解决这个错误。我知道我的问题有很多代码需要阅读,但我必须发布我的所有代码,你可以看看我的整个代码。
我的Stacktrace: -
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.vamediabox.vamediaboxapp, PID: 30915
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vamediabox.vamediaboxapp/com.vamediabox.vamediaboxapp.activities.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.vamediabox.vamediaboxapp.activities.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.vamediabox.vamediaboxapp.activities.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unexpected end of document
at android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:209)
at android.support.v7.view.SupportMenuInflater.inflate(SupportMenuInflater.java:122)
at android.support.design.widget.NavigationView.inflateMenu(NavigationView.java:250)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:174)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:100)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:289)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.vamediabox.vamediaboxapp.activities.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我的activity_main.xml: -
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_navigation_panel"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_navigation_panel"
app:itemTextColor="@color/black"
app:menu="@menu/main_drawer" />
</android.support.v4.widget.DrawerLayout>
我的app_bar_navigation_panel.xml: -
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context="com.vamediabox.vamediaboxapp.activities.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/red_button"
android:theme="@style/AppTheme.AppBarOverlay">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_navigation_panel" />
</android.support.design.widget.CoordinatorLayout>
我的content_navigation_panel.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical">
<FrameLayout
android:id="@+id/your_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />
</LinearLayout>
我的nav_header_navigation_panel.xml: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/background1"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:id="@+id/iv1"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv1"
android:layout_marginTop="15dp"
android:textSize="15sp"
android:text="www.vamediabox.com"
android:textColor="@color/white"/>
</RelativeLayout>
我的toobar.xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:background="@drawable/background1"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
我的main_drawer.xml: -
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:anroid="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="About Us" />
<item
android:id="@+id/nav_services"
android:icon="@drawable/ic_settings_black_24dp"
android:title="Services" />
<item
android:id="@+id/nav_Portfolio"
android:icon="@drawable/ic_card_travel_black_24dp"
android:title="Portfolio" />
<item
android:id="@+id/nav_contact"
android:icon="@drawable/ic_contact_mail_black_24dp"
android:title="Contact" />
</group>
<menu>
<item
android:id="@+id/nav_exit"
android:icon="@drawable/ic_exit_to_app_black_24dp"
android:title="Exit" />
</menu>
</menu>
my home_fragment.xml
<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"
android:background="@drawable/background1"
android:layout_marginTop="15dp"
tools:context="com.vamediabox.vamediaboxapp.activities.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ly_homeimage">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@drawable/socialmedia"
android:scaleType="fitXY"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ly_homeimage"
android:layout_margin="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="@color/aluminum"
android:text="@string/activity_home" />
</ScrollView>
</LinearLayout>
</RelativeLayout>
我的main_activity.java
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Fragment fragment;
FragmentManager fragmentManager;
Toolbar toolbar;
private Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
assert drawer != null;
drawer.setDrawerListener(toggle);
drawer.setScrimColor(Color.TRANSPARENT);
toggle.syncState();
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (drawer.isDrawerVisible(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
bundle = getIntent().getExtras();
fragment = new HomeFragment();
getSupportActionBar().setTitle("VAMEDIA BOX");
setFragment();
}
@Override
protected void onStop() {
super.onStop();
}
public void setFragment() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.your_placeholder, fragment).addToBackStack(null);
transaction.commit();
}
private void showMessageDialog(final boolean exit) {
//Exit Dialog
final android.app.Dialog popuplayout = new android.app.Dialog(MainActivity.this);
popuplayout.requestWindowFeature(Window.FEATURE_NO_TITLE);
popuplayout.setContentView(R.layout.exitdialog);
popuplayout.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btnYes = (Button) popuplayout.findViewById(R.id.btnYes);
Button btnNo = (Button) popuplayout.findViewById(R.id.btnNo);
TextView tittle = (TextView) popuplayout.findViewById(R.id.txtdialogtitle);
tittle.setText("");
TextView msg = (TextView) popuplayout.findViewById(R.id.txtdialogMsg);
if (!exit)
msg.setText("Do you want to exit from application?");
else
msg.setText("Do you want to exit from application?");
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
popuplayout.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(popuplayout.getWindow().getAttributes());
lp.width = (int) (width - (width * 0.20));
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
popuplayout.getWindow().setAttributes(lp);
btnYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popuplayout.cancel();
if (!exit) {
finish();
} else {
moveTaskToBack(true);
finish();
}
}
});
btnNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popuplayout.cancel();
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
showMessageDialog(true);
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
String title = "";
switch (item.getItemId()) {
case R.id.nav_home: {
fragment = new HomeFragment();
title = "VAMEDIA BOX ";
break;
}
case R.id.nav_exit: {
title = "VAMEDIA BOX";
showMessageDialog(true);
}
}
getSupportActionBar().setTitle(title);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
setFragment();
return true;
}
}
我的HomeFragment.java
public class HomeFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:1)
引起:java.lang.RuntimeException:意外的文档结束 在 android.support.v7.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:209)
根据官方doc
<menu>
Defines a Menu, which is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements.
像这样更改你的main_drawer.xml它将起作用
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:anroid="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home"/>
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="About Us"/>
<item
android:id="@+id/nav_services"
android:icon="@drawable/ic_settings_black_24dp"
android:title="Services"/>
<item
android:id="@+id/nav_Portfolio"
android:icon="@drawable/ic_card_travel_black_24dp"
android:title="Portfolio"/>
<item
android:id="@+id/nav_contact"
android:icon="@drawable/ic_contact_mail_black_24dp"
android:title="Contact"/>
</group>
<item
android:id="@+id/nav_exit"
android:icon="@drawable/ic_exit_to_app_black_24dp"
android:title="Exit"/>
</menu>