我想在手机上运行我的应用程序,但是启动屏幕加载完成后,我立即收到此错误。重申一下,应用程序可以在模拟器上正常运行。 仅当在真实设备上进行测试时,才会发生此错误。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lawrence.blmmobile/com.example.lawrence.blmmobile.MainActivity}: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class android.support.design.widget.NavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class android.support.design.widget.NavigationView
Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class android.support.design.widget.NavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:652)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.lawrence.blmmobile.MainActivity.onCreate(MainActivity.java:82)
at android.app.Activity.performCreate(Activity.java:6845)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
Caused by: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:652)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:211)
at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:267)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:176)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:98)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:652)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:812)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:752)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:883)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:846)
at android.view.LayoutInflater.inflate(LayoutInflater.java:522)
at android.view.LayoutInflater.inflate(LayoutInflater.java:430)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.lawrence.blmmobile.MainActivity.onCreate(MainActivity.java:82)
at android.app.Activity.performCreate(Activity.java:6845)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
我的activity_main文件,认为错误来自哪里。 `
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<AdapterViewFlipper
android:id="@+id/idAdapterViewFlipper"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp" />
<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_main"
app:menu="@menu/activity_main_drawer" />
` 我的MainActivity文件
`包com.example.lawrence.blmmobile;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterViewFlipper;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private AdapterViewFlipper adapterViewFlipper;
private static final String[] TEXT = { "The LORD is good unto them that wait for him (Lam.3:25)",
"Be of one mind, live in peace (2Cor.13:11)",
"But be ye doers of the word (James.1:22)",
"So be ye holy in all manner of conversation (1Pet.1:15)",
"Be ye holy; for I am holy (1Pet.1:16)",
"Hide me under the shadow of thy wings (Ps.17:8)",
"Great peace have they which love thy law (Ps.119:165)"};
private static final int[]IMAGES = {R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,
R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,
R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,
R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,
R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,
R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,R.drawable.b1,};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapterViewFlipper =(AdapterViewFlipper) findViewById(R.id.idAdapterViewFlipper);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//creating adapter object
FlipperAdapter adapter =new FlipperAdapter(this,TEXT,IMAGES);
adapterViewFlipper.setAdapter(adapter);
adapterViewFlipper.setAutoStart(true);
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);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
displaySelectedScreen(R.id.nav_home);
}
class FlipperAdapter extends BaseAdapter{
Context ctx;
int[]images;
String[] text;
LayoutInflater inflater;
public FlipperAdapter(Context context, String[]myText, int[]myImages){
this.ctx=context;
this.images=myImages;
this.text= myText;
inflater= LayoutInflater.from(context);
}
@Override
public int getCount(){
return text.length;
}
@Override
public Object getItem(int i){
return null;
}
@Override
public long getItemId(int i){
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup){
view= inflater.inflate(R.layout.flipper_items, null);
TextView txtName =(TextView) view.findViewById(R.id.idTextView);
ImageView txtImage =(ImageView) view.findViewById(R.id.idImageView);
txtName.setText(text[i]);
txtImage.setImageResource(images[i]);
TextView marque = (TextView) view.findViewById(R.id.marque_scrolling_text);
marque.setSelected(true);
return view;
}
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.nav_facebook:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, FacebookActivity.class));
return true;
case R.id.nav_brighterlife:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, AboutBrighterLifeActivity.class));
return true;
case R.id.nav_developer:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, DeveloperActivity.class));
return true;
case R.id.nav_feedback:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, FeedActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//calling the method displayselectedscreen and passing the id of selected menu
displaySelectedScreen(item.getItemId());
//make this method blank
return true;
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_home:
fragment = new Home();
break;
case R.id.nav_message:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, MessagesActivity.class));
break;
case R.id.nav_books:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, ScripturesActivity.class));
break;
case R.id.nav_wisdom:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, WordsActivity.class));
break;
case R.id.nav_prayer:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, PrayerActivity.class));
break;
case R.id.nav_feedback:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, FeedActivity.class));
break;
case R.id.nav_brighterlife:
// launch new intent instead of loading fragment
startActivity(new Intent(MainActivity.this, AboutBrighterLifeActivity.class));
break;
case R.id.nav_about:
fragment = new About();
break;
case R.id.nav_exit:
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit!")
.setMessage("Are you sure you want to close?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mainlayout, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
}
`
请帮帮我。