我创建了一个小型导航抽屉应用程序,以测试ui如何查找项目,并且构建成功,但是在手机甚至虚拟设备上打开该应用程序时,该应用程序都会崩溃。 ps。该应用程序设置为可以在手机API 21及更高版本上运行,并且我的手机和虚拟设备都属于此类别,因此应该可以运行。
我曾尝试删除一些我认为可能是造成这种情况的罪魁祸首的代码,以使应用程序在旋转和其他操作时不会变回主屏幕。
这是mainactivity.java文件
package com.example.waterusagerecorder;
import android.graphics.ImageFormat;
import android.graphics.drawable.AnimationDrawable;
import android.media.Image;
import android.support.annotation.NonNull;
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.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
AnimationDrawable wateranimation;
AnimationDrawable baranimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
ImageView imageView = (ImageView) findViewById(R.id.wateranim);
imageView.setBackgroundResource(R.drawable.animation);
wateranimation = (AnimationDrawable) imageView.getBackground();
ImageView imageView1 = (ImageView) findViewById(R.id.baranim);
imageView1.setBackgroundResource(R.drawable.barmation);
baranimation = (AnimationDrawable) imageView1.getBackground();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_mywater:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MyWaterFragment()).commit();
break;
case R.id.nav_appliances:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AppliancesFragment()).commit();
break;
case R.id.nav_history:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HistoryFragment()).commit();
break;
case R.id.nav_water_facts:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new WaterFactsFragment()).commit();
break;
case R.id.nav_settings:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new SettingsFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
wateranimation.start();
baranimation.start();
}
}
该应用看上去应与本视频开头所示的应用有些相似,但也带有一些动画。 Video
p.s。请在评论中告诉我是否需要像其他文件一样提供更多信息,例如布局文件或菜单文件或其他任何文件。不幸的是,我不知道在手机或虚拟设备上哪里可以找到崩溃日志或报告。如果您知道,也请在下面告诉我
如mike.m所述,这是崩溃的完整堆栈跟踪
2019-04-26 17:33:39.030 8310-8310/com.example.waterusagerecorder I/art: Not late-enabling -Xcheck:jni (already on)
2019-04-26 17:33:39.034 8310-8310/com.example.waterusagerecorder W/art: Unexpected CPU variant for X86 using defaults: x86
2019-04-26 17:33:39.431 8310-8310/com.example.waterusagerecorder W/System: ClassLoader referenced unknown path: /data/app/com.example.waterusagerecorder-1/lib/x86
2019-04-26 17:33:39.466 8310-8310/com.example.waterusagerecorder I/InstantRun: starting instant run server: is main process
2019-04-26 17:33:39.675 8310-8310/com.example.waterusagerecorder W/art: Verification of java.lang.Object com.example.waterusagerecorder.MainActivity.access$super(com.example.waterusagerecorder.MainActivity, java.lang.String, java.lang.Object[]) took 100.922ms
2019-04-26 17:33:39.950 8310-8310/com.example.waterusagerecorder W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
2019-04-26 17:33:40.098 8310-8310/com.example.waterusagerecorder I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener;
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, android.support.v4.view.OnApplyWindowInsetsListener) (ViewCompat.java:2203)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:637)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void com.example.waterusagerecorder.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:26)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6662)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2599)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2707)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1460)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.os.Looper.loop() (Looper.java:154)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6077)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:866)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:756)
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.View$OnUnhandledKeyEventListener" on path: DexPathList[[zip file "/data/app/com.example.waterusagerecorder-1/base.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.waterusagerecorder-1/split_lib_slice_8_apk.apk", zip file "/
2019-04-26 17:33:40.099 8310-8310/com.example.waterusagerecorder I/art: at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:380)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:312)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, android.support.v4.view.OnApplyWindowInsetsListener) (ViewCompat.java:2203)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at android.view.ViewGroup android.support.v7.app.AppCompatDelegateImpl.createSubDecor() (AppCompatDelegateImpl.java:637)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor() (AppCompatDelegateImpl.java:518)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v7.app.AppCompatDelegateImpl.setContentView(int) (AppCompatDelegateImpl.java:466)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at void android.support.v7.app.AppCompatActivity.setContentView(int) (AppCompatActivity.java:140)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at void com.example.waterusagerecorder.MainActivity.onCreate(android.os.Bundle) (MainActivity.java:26)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at void android.app.Activity.performCreate(android.os.Bundle) (Activity.java:6662)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at void android.app.Instrumentation.callActivityOnCreate(android.app.Activity, android.os.Bundle) (Instrumentation.java:1118)
2019-04-26 17:33:40.100 8310-8310/com.example.waterusagerecorder I/art: at android.app.Activity android.app.ActivityThread.performLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent) (ActivityThread.java:2599)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread.handleLaunchActivity(android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:2707)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread.-wrap12(android.app.ActivityThread, android.app.ActivityThread$ActivityClientRecord, android.content.Intent, java.lang.String) (ActivityThread.java:-1)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread$H.handleMessage(android.os.Message) (ActivityThread.java:1460)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:102)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void android.os.Looper.loop() (Looper.java:154)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:6077)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at java.lang.Object java.lang.reflect.Method.invoke!(java.lang.Object, java.lang.Object[]) (Method.java:-2)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run() (ZygoteInit.java:866)
2019-04-26 17:33:40.101 8310-8310/com.example.waterusagerecorder I/art: at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:756)
2019-04-26 17:33:41.041 8310-8310/com.example.waterusagerecorder D/AndroidRuntime: Shutting down VM
2019-04-26 17:33:41.041 8310-8310/com.example.waterusagerecorder E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.waterusagerecorder, PID: 8310
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waterusagerecorder/com.example.waterusagerecorder.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
at com.example.waterusagerecorder.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
我试图通过在activity_main.xml文件中包含主要片段的布局来修复它,但这只是创建了覆盖效果,如屏幕截图所示。如果有人知道如何解决我的问题,请在可能的情况下通过评论告知我image of the weird overlaying effect
答案 0 :(得分:-1)
可能是由于多种原因。 请在清单文件中声明要加载的每个类(作为MainActivity类)。 也可能是由于您正在加载的图像尺寸太大。