Исключение нулевого указателя только при загрузке из Google Play - PullRequest
0 голосов
/ 04 ноября 2019

Я получаю исключение нулевого указателя в моем приложении для Android:


java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at a.a.a.a.l.b(SourceFile:1)
    at a.a.a.b.c$a.c(SourceFile:9)
    at a.a.a.b.c.b(SourceFile:29)
    at androidx.recyclerview.widget.RecyclerView$f.a(SourceFile:14)
    at androidx.recyclerview.widget.RecyclerView$u.a(SourceFile:162)

У меня есть приложение для Android с тремя фрагментами и нижней панелью, которая отображает некоторую информацию из базы данных Firebase. Приложение уже находится в Google Play, и в предварительном отчете показывается исключение. Я проверяю свой код и добавляю if перед вызовом метода equals во всех строковых объектах, вызывая метод, только если объект не равен NULL. Хуже всего то, что когда я проверяю на своем устройстве (с приложением, установленным из студии android) возможность отладки, приложение не падает. Но если я загружаю приложение из Google Play, оно вылетает.

Ниже находятся журнал и другие файлы

public class SpecialsFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {


    private SpecialsViewModel specialsViewModel;
    private ImageView mExpandedImage;
    private SwipeRefreshLayout refreshLayout;
    private ExpandableSpecialsAdapter specialsAdapter;
    private RecyclerView specialsRecyclerView;
    private RecyclerView.LayoutManager layoutManager;

    public View onCreateView( @NonNull LayoutInflater inflater,
                              ViewGroup container, Bundle savedInstanceState ) {

        ((Main2Activity)getActivity()).fragmentState = MainActivityNavigationState.ShowingSpecials;

        specialsViewModel =
                ViewModelProviders.of(this).get(SpecialsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_specials, container, false);

        refreshLayout = root.findViewById(R.id.swipe_refresh);
        refreshLayout.setOnRefreshListener(this);
        mExpandedImage = ((Main2Activity)getActivity()).getExpandedImage();

        specialsRecyclerView = root.findViewById(R.id.specials_recyclerview);
        specialsAdapter = new ExpandableSpecialsAdapter(getContext(), mExpandedImage);
        layoutManager = new LinearLayoutManager(getContext());

        specialsRecyclerView.setLayoutManager(layoutManager);
        specialsRecyclerView.setAdapter(specialsAdapter);

        return root;
    }

    private Specials getSpecial( QueryDocumentSnapshot document ) {
        String name = document.get("name").toString();
        String  where = document.get("where").toString();
        String fromDay = document.get("from_day").toString();
        String toDay = document.get("to_day").toString();
        String fromTime = document.get("from_hour").toString();
        String toTime = document.get("to_hour").toString();
        String pic = document.get("picture").toString();
        String description = document.get("description").toString();
        return new Specials(name, where, fromDay, toDay, fromTime, toTime, pic, description);
    }


    @Override
    public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {
        super.onViewCreated(view, savedInstanceState);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        refreshLayout.setRefreshing(true);
        specialsViewModel.getAllSpecials().observe(this, new Observer<List<ListItem>>() {
            @Override
            public void onChanged( List<ListItem> listItems ) {
                if (listItems.size()>0)
                    specialsAdapter.setData(listItems);
                else {

                    specialsAdapter.setData(new ArrayList<ListItem>());
                    Toast.makeText(getContext(), "Sorry, we don't have any specials at the moment", Toast.LENGTH_LONG).show();
                }
                refreshLayout.setRefreshing(false);

            }

        });
    }

}

FATAL EXCEPTION: ControllerMessenger
Process: com.rp.myrestaurants, PID: 14148
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
    at a.a.a.a.l.b(SourceFile:1)
    at a.a.a.b.c$a.c(SourceFile:9)
    at a.a.a.b.c.b(SourceFile:29)
    at androidx.recyclerview.widget.RecyclerView$f.a(SourceFile:14)
    at androidx.recyclerview.widget.RecyclerView$u.a(SourceFile:162)
    at androidx.recyclerview.widget.LinearLayoutManager$c.a(SourceFile:10)
    at androidx.recyclerview.widget.LinearLayoutManager.a(SourceFile:107)
    at androidx.recyclerview.widget.LinearLayoutManager.a(SourceFile:94)
    at androidx.recyclerview.widget.LinearLayoutManager.c(SourceFile:126)
    at androidx.recyclerview.widget.RecyclerView.f(SourceFile:8)
    at androidx.recyclerview.widget.RecyclerView.d(SourceFile:9)
    at androidx.recyclerview.widget.RecyclerView.onLayout(SourceFile:2)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at androidx.swiperefreshlayout.widget.SwipeRefreshLayout.onLayout(SourceFile:11)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at androidx.constraintlayout.widget.ConstraintLayout.onLayout(SourceFile:12)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(SourceFile:13)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
    at android.view.View.layout(View.java:16651)
    at android.view.ViewGroup.layout(ViewGroup.java:5441)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1943)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at androidx.test.espresso.base.Interrogator.a(Interrogator.java:19)
    at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:169)
    at androidx.test.espresso.base.UiControllerImpl.a(UiControllerImpl.java:161)
    at androidx.test.espresso.base.UiCont

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
//apply plugin: 'io.fabric'
apply plugin: "androidx.navigation.safeargs"



android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.rp.myrestaurants"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 14
        versionName "1.3"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true
            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true
        }

    }

    compileOptions {
        targetCompatibility = "1.8"
        sourceCompatibility = "1.7"
    }
    buildToolsVersion = '29.0.2'


}

dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
    implementation 'androidx.navigation:navigation-fragment:2.1.0'
    implementation 'androidx.navigation:navigation-ui:2.1.0'
    testImplementation 'junit:junit:4.13-beta-3'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'





    implementation 'com.google.firebase:firebase-core:17.2.0'
    implementation 'com.google.firebase:firebase-firestore:21.1.0'
    implementation 'com.google.firebase:firebase-messaging:20.0.0'
    implementation 'com.google.firebase:firebase-database:19.1.0'

    //crahslitics dependecy
    // (Recommended) Add Analytics
    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    // Add dependency


    implementation 'com.squareup.picasso:picasso:2.71828'


    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    def nav_version = "2.1.0-rc01"

    // Java
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    implementation "com.ablanco.zoomy:zoomy:1.1.0"
    implementation 'com.google.android.gms:play-services-location:17.0.0'

    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'


    implementation 'androidx.appcompat:appcompat:1.1.0'


    // Extensions = ViewModel + LiveDat



    implementation group: 'androidx.room', name: 'room-runtime', version: '2.1.0'
    //implementation "android.arch.persistence.room:compiler:2.1.0"
    annotationProcessor group: 'androidx.room', name: 'room-compiler', version: '2.1.0'

    androidTestImplementation "android.arch.persistence.room:testing:2.1.0"


    implementation "androidx.core:core-ktx:+"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"


}

apply plugin: 'com.google.gms.google-services'
repositories {
    mavenCentral()
}

1 Ответ

0 голосов
/ 05 ноября 2019

После целой ночи поиска и изучения кода и благодаря вопросу о @Ehsan msz я обнаружил проблему. Кажется, что оптимизация кода удаляет какой-то важный код.

Я просто удалил это из Gradle, и теперь все в порядке.

buildTypes {
        release {
            // Enables code shrinking, obfuscation, and optimization for only
            // your project's release build type.
            minifyEnabled true
            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true
        }

    }

Спасибо всем вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...