致命异常:Android Studio中的主要错误

时间:2018-08-16 11:12:10

标签: java android android-studio

我最近更新了代码,然后运行该程序只是为了在应用程序启动和下一秒钟关闭时看到此错误。 以前的版本运行正常,更新后出现了问题。

  

08-16 16:23:29.883 13995-13995 /? E / AndroidRuntime:致命异常:main       流程:com.example.hemanth.anew,PID:13995       java.lang.RuntimeException:无法启动活动       ComponentInfo {com.example.hemanth.anew / com.example.hemanth.anew。       HomeActivity}:       java.lang.NullPointerException:尝试在空对象上调用虚拟方法“ java.lang.Class java.lang.Object.getClass()”   参考           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)           在android.app.ActivityThread.access $ 900(ActivityThread.java:154)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5292)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)        原因:java.lang.NullPointerException:尝试在null上调用虚拟方法'java.lang.Class java.lang.Object.getClass()'   对象参考           在com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.getBottomNavigationItemViews(BottomNavigationViewEx.java:569)处           在com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx.enableAnimation(BottomNavigationViewEx.java:341)处           在com.example.hemanth.anew.Utils.BottomNavigationViewHelper.setupBottomNavigationView(BottomNavigationViewHelper.java:11)           在com.example.hemanth.anew.HomeActivity.setupBottomNavigationView(HomeActivity.java:25)           在com.example.hemanth.anew.HomeActivity.onCreate(HomeActivity.java:19)           在android.app.Activity.performCreate(Activity.java:5990)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)           在android.app.ActivityThread.access $ 900(ActivityThread.java:154)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1321)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:135)           在android.app.ActivityThread.main(ActivityThread.java:5292)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

这是我的家庭活动代码:

package com.example.hemanth.anew;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.hemanth.anew.Utils.BottomNavigationViewHelper;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class HomeActivity extends AppCompatActivity {

private static final String TAG = "HomeActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Log.d(TAG, "onCreate: starting.");
    setupBottomNavigationView();
}

private void setupBottomNavigationView(){
    Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView");
    BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar);
    BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);

  }
}

有人可以帮助我克服此错误吗? 检查此BottomNavigationViewHelper.java文件:

package com.example.hemanth.anew.Utils;
import android.annotation.SuppressLint;
import android.util.Log;

import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;

public class BottomNavigationViewHelper {
private static final String TAG = "BottomNavigationViewHel";
@SuppressLint("LongLogTag")
public static void setupBottomNavigationView(BottomNavigationViewEx 
bottomNavigationViewEx){
    Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView");
    bottomNavigationViewEx.enableAnimation(false);
    bottomNavigationViewEx.enableItemShiftingMode(false);
    bottomNavigationViewEx.enableShiftingMode(false);
    bottomNavigationViewEx.setTextVisibility(false);
}
}

这是菜单部分下的xml文件

<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true">

    <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/bottomNavViewBar"
        android:background="@drawable/white_grey_border_top"
        app:menu="@menu/bottom_navigation_menu">


    </com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>

</RelativeLayout>



</merge>

这是activity_home.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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=".HomeActivity">


 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!-- top toolbar -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relLayout1">

        <include layout="@layout/layout_top_tabs"/>

    </RelativeLayout>

    <!-- Middle section (body) -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relLayout2"
        android:layout_below="@+id/relLayout1">

        <include layout="@layout/layout_center_viewpager"/>


    </RelativeLayout>


    <!-- bottom navigation -->
    <include layout="@layout/layout_bottom_navigation_view"/>


</RelativeLayout>


</android.support.design.widget.CoordinatorLayout>

这是bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/ic_house"
    android:icon="@drawable/ic_house"
    android:title="">
</item>

<item
    android:id="@+id/ic_search"
    android:icon="@drawable/ic_search"
    android:title="">
</item>

<item
    android:id="@+id/ic_circle"
    android:icon="@drawable/ic_circle"
    android:title="">
</item>

<item
    android:id="@+id/ic_alert"
    android:icon="@drawable/ic_alert"
    android:title="">
</item>

<item
    android:id="@+id/ic_android"
    android:icon="@drawable/ic_android"
    android:title="">
</item>

</menu>

感谢所有答案,我在最新版本的android studio中找到了解决此问题的简便方法。它是这样的:将其添加到BotttomNavigationView.xml文件

app:labelVisibilityMode="unlabeled" 

4 个答案:

答案 0 :(得分:0)

您在NullPointerException方法中有一个setupBottomNavigationView()。 似乎未检索到bottomNavigationViewEx

答案 1 :(得分:0)

这行似乎导致了header = dashboardHeader(title = div(dashboard_title, style="text-align: left;")) header$children[[3]]$children <- tags$img(src=logo_path, align="right", height='50px')

NullPointerException

您确定BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx); 具有一些有效值吗?

编辑:

好的,我明白了错误所在。我尝试了您的代码,发现在compileSdkVersion 27之前,它可以正常工作。您再往前走,即现在28,您会得到一个错误。这是因为库的作者为26-check here构建了库。它适用于27,因为他用于与27兼容的代码没有太大变化。但是当28发布时,它不受支持。作者必须为SDK 28更新他的库。还剩下以下选项:

  • 可以将项目降级为使用BottomNavigationViewEx的版本(代码为SDK 26,但SDK 27应该也可以正常工作)
  • 使用BottomNavigationViewEx以外的其他项目
  • 请BottomNavigationViewEx开发人员升级其项目
  • 将BottomNavigationViewEx导入项目并重写有问题的方法 你自己

答案 2 :(得分:-1)

检查此示例:

步骤1。将其添加到存储库末尾的root build.gradle中:

allprojects {
repositories {
    ...
    maven { url "https://jitpack.io" }
}

}

第2步。添加依赖项

compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.2'

对于Maven:

步骤13。将其添加到存储库末尾的root build.gradle中:

<repositories>
 <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
 </repository>
</repositories>

第4步。添加依赖项

<dependency>
 <groupId>com.github.ittianyu</groupId>
 <artifactId>BottomNavigationViewEx</artifactId>
 <version>1.1.2</version>
</dependency>

第5步。 BottomNavigationViewEx.java并将其复制到您的项目中。

第6步。在xml中添加自定义窗口小部件:

<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
 android:id="@+id/bnve"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:background="@color/colorPrimary"
 app:itemIconTint="@color/selector_item_color"
 app:itemTextColor="@color/selector_item_color"
 app:menu="@menu/menu_navigation_with_view_pager" />

步骤7。在“活动”中绑定视图:

BottomNavigationViewEx bnve = (BottomNavigationViewEx) findViewById(R.id.bnve);

第8步。禁用所有动画

bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);

第9步。与ViewPager绑定

// set adapter
adapter = new VpAdapter(getSupportFragmentManager(), fragments);
bind.vp.setAdapter(adapter);

// binding with ViewPager
bind.bnve.setupWithViewPager(bind.vp);

步骤10 .ProGuard

如果您使用的是ProGuard,则可能需要添加以下选项:

-keep public class android.support.design.widget.BottomNavigationView { *; }
-keep public class android.support.design.internal.BottomNavigationMenuView { *; }
-keep public class android.support.design.internal.BottomNavigationPresenter { *; }
-keep public class android.support.design.internal.BottomNavigationItemView { *; }

答案 3 :(得分:-1)

如果不是静态的,则必须创建BottomNavigationViewHelper类的实例。作为Null指针,异常是从那里开始的,这是因为如果未初始化BottomNavigationViewHelper。因此,以此更新您的代码,然后查看错误消息。

   private void setupBottomNavigationView(){
        Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView");
        BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper helper=new BottomNavigationViewHelper();
helper.setupBottomNavigationView(bottomNavigationViewEx);

      }