1

I wonder if anyone could advise as to why

fragmentList=getSupportFragmentManager().getFragments();" 

in the code is returning null in the following code:

selectedspecies.setData(ArrayChar); - which generates a null pointer exception

and likewise with

morphologicalcharacters.setSpeciesSelected(false); (which also generates a null pointer exception)

. I have tested via USB cable on a range of devices which work well but crash on customer phones.

public void SendData(String[] ArrayChar) //Receives selection from       Morphological Characters and sends it to Selected Species Fragment
{
   this.ArrayChar=ArrayChar;
   List<Fragment> fragmentList;
   fragmentList=getSupportFragmentManager().getFragments();
   int fragmentListSizeInt=fragmentList.size();
   String fragmentListSizeString=""+fragmentListSizeInt;
   Fragment morphfrag=getSupportFragmentManager().findFragmentById(morphid);

   //Fragment frag1=GlobalPageAdapter.getFragment(0);
   //Fragment frag2=GlobalPageAdapter.getFragment(1);

   //Toast.makeText(getApplicationContext(),"Error: Fragment List Size is: "+fragmentListSizeString, Toast.LENGTH_LONG).show(); //Test Code
    //https://developer.android.com/training/basics/fragments/communicating
   if (fragmentList.size()<1)
   {
       Toast.makeText(getApplicationContext(),"Error: Fragment List Array is empty - report error to Ecological Software Solutions at ecologicalsoftwaresolutions@gmail.com",Toast.LENGTH_LONG).show(); //Test Code
   }
   if (!SpeciesSelected)
   {
       int x=GlobalViewPager.getAdapter().getCount();
       SelectedSpecies selectedspecies=null;
       MorphologicalCharacters morphologicalcharacters=null;
       for (int xx = 0; xx < fragmentList.size(); xx++)
       {
          Object object = fragmentList.get(xx);
          String classobject = object.getClass().toString();
          if (classobject.equals("class com.ebookfrenzy.GramineaeIdentification.MorphologicalCharacters")) {
              morphologicalcharacters = (MorphologicalCharacters) fragmentList.get(xx);

          }
       }
       if (morphologicalcharacters!=null) {
           morphologicalcharacters.setSpeciesSelected(false);
       }
       else
       {
           MorphologicalCharacters newFragment=new MorphologicalCharacters();
           Bundle args=new Bundle();
           args.putBoolean("0",false);
           newFragment.setArguments(args);
           FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
           transaction.replace(R.id.container,newFragment);
           transaction.addToBackStack(null);
           transaction.commit();
           Toast.makeText(getApplicationContext(),"Morphological Character Fragment is null - report error to Ecological Software Solutions at ecologicalsoftwaresolutions@gmail.com",Toast.LENGTH_LONG).show();
       }

       for (int xx = 0; xx < fragmentList.size(); xx++) {
            Object object = fragmentList.get(xx);
            String classobject = object.getClass().toString();
            //Toast.makeText(getApplicationContext(),classobject,Toast.LENGTH_LONG).show(); //Test Code
            if (classobject.equals("class com.ebookfrenzy.GramineaeIdentification.SelectedSpecies")) {
                selectedspecies = (SelectedSpecies) fragmentList.get(xx);
            }
       }

       if (selectedspecies==null)
       {
           SelectedSpecies newFragment=new SelectedSpecies();
           Bundle args=new Bundle();
           args.putStringArray("1",ArrayChar);
           newFragment.setArguments(args);
           FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
           transaction.replace(R.id.container,newFragment);
           transaction.addToBackStack(null);
           transaction.commit();

           if (fragmentList.size()<1)
           {
               Toast.makeText(getApplicationContext(),"Error: Fragment List Array only has one fragment - report error to Ecological Software Solutions at ecologicalsoftwaresolutions@gmail.com",Toast.LENGTH_LONG).show();Toast.makeText(getApplicationContext(),"Error: Fragment List Array only has one fragment - report error to Ecological Software Solutions at ecologicalsoftwaresolutions@gmail.com",Toast.LENGTH_LONG).show();
           }
           Toast.makeText(getApplicationContext(),"Error: Selected Species Fragment is null - report error to Ecological Software Solutions at ecologicalsoftwaresolutions@gmail.com",Toast.LENGTH_LONG).show();
           //https://developer.android.com/reference/android/support/v4/app/FragmentManager
       }
       if (selectedspecies!=null)
       {
           selectedspecies.setData(ArrayChar);
       }
   }
}

My Grade files are as follows - am I missing anything in the code:

apply plugin: 'com.android.application'

android {
    signingConfigs {
        config {
            keyAlias [reacted]
            keyPassword [reacted]
            storeFile file('C:/Users/ajgra/Androidkeystore/release.keystore.jks')
            storePassword [reacted]
        }
    }
    compileSdkVersion 26
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "com.ebookfrenzy.tablayoutdemo"
        minSdkVersion 15
        targetSdkVersion 26
        multiDexEnabled true
        versionCode 16
        versionName "1.16"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    apply plugin: 'build-announcements'
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:cardview-v7:26.1.0'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:multidex:1.0.3'
    //compile 'com.google.android.gms:play-services-ads:11.6.2'
    testImplementation 'junit:junit:4.12'
}

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
           url "https://maven.google.com"
           }
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I am at a lost at how to fix it. Moreover, the app is also generating all sorts of strange errors in the Google Play Developer console but not in the test devices which uses the debug apk via usb cable.

The fault seems to be in the release apk rather than the debug apk. Is this something to do with ProGuard and should I disable proguard?

Many thanks, Alasdair Grant

0 Answers0