这是我的MainActivity.java的代码。在编译时,它没有显示任何编译错误,但每当我使用AVD运行它时,它在启动应用程序时崩溃。
package com.example.somanshusrivastava.carpiko;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
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;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar=null;
NavigationView navigationView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment, "FRAGMENT");
fragmentTransaction.commit();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
AboutFragment fragment = new AboutFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment, "FRAGMENT");
fragmentTransaction.commit();
} else if (id == R.id.nav_manage) {
SettingFragment fragment = new SettingFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment, "FRAGMENT");
fragmentTransaction.commit();
} else if (id == R.id.nav_share) {
ContactFragment fragment = new ContactFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.drawer_layout, fragment, "FRAGMENT");
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这里 drawer_layout 是main_activity.xml中id的名称。 Drawer_layout用作R.id.drawer_layout代替标准fragment_container,因为fragment_container未被解析为符号。无效缓存和重新启动没有帮助。
我应该使用其他东西代替drawer_layout ID吗?或者代码中是否存在逻辑错误?
忽视相机中的碎片名称,发送,分享
更新部分
11-23 10:38:04.430 2095-2095/? I/art: Not late-enabling -Xcheck:jni (already on)
11-23 10:38:04.430 2095-2095/? I/art: Late-enabling JIT
11-23 10:38:04.433 2095-2095/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
11-23 10:38:04.830 2095-2095/com.example.somanshusrivastava.carpiko W/System: ClassLoader referenced unknown path: /data/app/com.example.somanshusrivastava.carpiko-2/lib/x86
11-23 10:38:05.697 2095-2106/com.example.somanshusrivastava.carpiko I/art: Background partial concurrent mark sweep GC freed 96(3KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 2.581ms total 140.616ms
11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.812 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.827 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc sticky concurrent mark sweep GC freed 79(11KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 5.901ms total 13.614ms
11-23 10:38:05.836 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.850 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc partial concurrent mark sweep GC freed 23(1008B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 2.153ms total 10.247ms
11-23 10:38:05.853 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.867 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 15(12KB) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.431ms total 13.789ms
11-23 10:38:05.868 2095-2095/com.example.somanshusrivastava.carpiko I/art: Forcing collection of SoftReferences for 49MB allocation
11-23 10:38:05.869 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.881 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 11(344B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.407ms total 11.758ms
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko W/art: Throwing OutOfMemoryError "Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM"
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.882 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.892 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc sticky concurrent mark sweep GC freed 3(608B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.389ms total 8.964ms
11-23 10:38:05.894 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.903 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc partial concurrent mark sweep GC freed 6(192B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.387ms total 8.867ms
11-23 10:38:05.904 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.916 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.416ms total 10.922ms
11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: Forcing collection of SoftReferences for 49MB allocation
11-23 10:38:05.917 2095-2095/com.example.somanshusrivastava.carpiko I/art: Starting a blocking GC Alloc
11-23 10:38:05.928 2095-2095/com.example.somanshusrivastava.carpiko I/art: Alloc concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 7% free, 50MB/54MB, paused 1.350ms total 10.018ms
11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko W/art: Throwing OutOfMemoryError "Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM"
11-23 10:38:05.929 2095-2095/com.example.somanshusrivastava.carpiko D/skia: --- allocation failed for scaled bitmap
11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko D/AndroidRuntime: Shutting down VM
11-23 10:38:05.930 2095-2095/com.example.somanshusrivastava.carpiko E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.somanshusrivastava.carpiko, PID: 2095
java.lang.OutOfMemoryError: Failed to allocate a 51750048 byte allocation with 4194304 free bytes and 13MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)
at android.content.res.Resources.loadDrawable(Resources.java:2540)
at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
at android.widget.ImageView.(ImageView.java:152)
at android.widget.ImageView.(ImageView.java:140)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:58)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:54)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:95)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:938)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:992)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.somanshusrivastava.carpiko.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-23 10:38:07.560 2095-2102/com.example.somanshusrivastava.carpiko W/art: Suspending all threads took: 13.233ms
11-23 10:38:09.537 2095-2102/com.example.somanshusrivastava.carpiko W/art: Suspending all threads took: 9.664ms
11-23 10:38:11.140 2095-2095/? I/Process: Sending signal. PID: 2095 SIG: 9